2 * Resizable virtual memory filesystem for Linux.
4 * Copyright (C) 2000 Linus Torvalds.
6 * 2000-2001 Christoph Rohland
9 * Copyright (C) 2002-2005 Hugh Dickins.
10 * Copyright (C) 2002-2005 VERITAS Software Corporation.
11 * Copyright (C) 2004 Andi Kleen, SuSE Labs
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>
18 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20 * This file is released under the GPL.
24 #include <linux/init.h>
25 #include <linux/vfs.h>
26 #include <linux/mount.h>
27 #include <linux/pagemap.h>
28 #include <linux/file.h>
30 #include <linux/module.h>
31 #include <linux/percpu_counter.h>
32 #include <linux/swap.h>
34 static struct vfsmount *shm_mnt;
38 * This virtual memory filesystem is heavily based on the ramfs. It
39 * extends ramfs by the ability to use swap and honor resource limits
40 * which makes it a completely usable filesystem.
43 #include <linux/xattr.h>
44 #include <linux/exportfs.h>
45 #include <linux/posix_acl.h>
46 #include <linux/generic_acl.h>
47 #include <linux/mman.h>
48 #include <linux/string.h>
49 #include <linux/slab.h>
50 #include <linux/backing-dev.h>
51 #include <linux/shmem_fs.h>
52 #include <linux/writeback.h>
53 #include <linux/blkdev.h>
54 #include <linux/security.h>
55 #include <linux/swapops.h>
56 #include <linux/mempolicy.h>
57 #include <linux/namei.h>
58 #include <linux/ctype.h>
59 #include <linux/migrate.h>
60 #include <linux/highmem.h>
61 #include <linux/seq_file.h>
62 #include <linux/magic.h>
64 #include <asm/uaccess.h>
65 #include <asm/div64.h>
66 #include <asm/pgtable.h>
69 * The maximum size of a shmem/tmpfs file is limited by the maximum size of
70 * its triple-indirect swap vector - see illustration at shmem_swp_entry().
72 * With 4kB page size, maximum file size is just over 2TB on a 32-bit kernel,
73 * but one eighth of that on a 64-bit kernel. With 8kB page size, maximum
74 * file size is just over 4TB on a 64-bit kernel, but 16TB on a 32-bit kernel,
75 * MAX_LFS_FILESIZE being then more restrictive than swap vector layout.
77 * We use / and * instead of shifts in the definitions below, so that the swap
78 * vector can be tested with small even values (e.g. 20) for ENTRIES_PER_PAGE.
80 #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
81 #define ENTRIES_PER_PAGEPAGE ((unsigned long long)ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
83 #define SHMSWP_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
84 #define SHMSWP_MAX_BYTES (SHMSWP_MAX_INDEX << PAGE_CACHE_SHIFT)
86 #define SHMEM_MAX_BYTES min_t(unsigned long long, SHMSWP_MAX_BYTES, MAX_LFS_FILESIZE)
87 #define SHMEM_MAX_INDEX ((unsigned long)((SHMEM_MAX_BYTES+1) >> PAGE_CACHE_SHIFT))
89 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
90 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
92 /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
93 #define SHMEM_PAGEIN VM_READ
94 #define SHMEM_TRUNCATE VM_WRITE
96 /* Definition to limit shmem_truncate's steps between cond_rescheds */
97 #define LATENCY_LIMIT 64
99 /* Pretend that each entry is of this size in directory's i_size */
100 #define BOGO_DIRENT_SIZE 20
103 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
104 char *name; /* xattr name */
109 /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
111 SGP_READ, /* don't exceed i_size, don't allocate page */
112 SGP_CACHE, /* don't exceed i_size, may allocate page */
113 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
114 SGP_WRITE, /* may exceed i_size, may allocate page */
118 static unsigned long shmem_default_max_blocks(void)
120 return totalram_pages / 2;
123 static unsigned long shmem_default_max_inodes(void)
125 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
129 static int shmem_getpage(struct inode *inode, unsigned long idx,
130 struct page **pagep, enum sgp_type sgp, int *type);
132 static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
135 * The above definition of ENTRIES_PER_PAGE, and the use of
136 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
137 * might be reconsidered if it ever diverges from PAGE_SIZE.
139 * Mobility flags are masked out as swap vectors cannot move
141 return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
142 PAGE_CACHE_SHIFT-PAGE_SHIFT);
145 static inline void shmem_dir_free(struct page *page)
147 __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
150 static struct page **shmem_dir_map(struct page *page)
152 return (struct page **)kmap_atomic(page, KM_USER0);
155 static inline void shmem_dir_unmap(struct page **dir)
157 kunmap_atomic(dir, KM_USER0);
160 static swp_entry_t *shmem_swp_map(struct page *page)
162 return (swp_entry_t *)kmap_atomic(page, KM_USER1);
165 static inline void shmem_swp_balance_unmap(void)
168 * When passing a pointer to an i_direct entry, to code which
169 * also handles indirect entries and so will shmem_swp_unmap,
170 * we must arrange for the preempt count to remain in balance.
171 * What kmap_atomic of a lowmem page does depends on config
172 * and architecture, so pretend to kmap_atomic some lowmem page.
174 (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
177 static inline void shmem_swp_unmap(swp_entry_t *entry)
179 kunmap_atomic(entry, KM_USER1);
182 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
184 return sb->s_fs_info;
188 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
189 * for shared memory and for shared anonymous (/dev/zero) mappings
190 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
191 * consistent with the pre-accounting of private mappings ...
193 static inline int shmem_acct_size(unsigned long flags, loff_t size)
195 return (flags & VM_NORESERVE) ?
196 0 : security_vm_enough_memory_kern(VM_ACCT(size));
199 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
201 if (!(flags & VM_NORESERVE))
202 vm_unacct_memory(VM_ACCT(size));
206 * ... whereas tmpfs objects are accounted incrementally as
207 * pages are allocated, in order to allow huge sparse files.
208 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
209 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
211 static inline int shmem_acct_block(unsigned long flags)
213 return (flags & VM_NORESERVE) ?
214 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
217 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
219 if (flags & VM_NORESERVE)
220 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
223 static const struct super_operations shmem_ops;
224 static const struct address_space_operations shmem_aops;
225 static const struct file_operations shmem_file_operations;
226 static const struct inode_operations shmem_inode_operations;
227 static const struct inode_operations shmem_dir_inode_operations;
228 static const struct inode_operations shmem_special_inode_operations;
229 static const struct vm_operations_struct shmem_vm_ops;
231 static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
232 .ra_pages = 0, /* No readahead */
233 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
236 static LIST_HEAD(shmem_swaplist);
237 static DEFINE_MUTEX(shmem_swaplist_mutex);
239 static void shmem_free_blocks(struct inode *inode, long pages)
241 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
242 if (sbinfo->max_blocks) {
243 percpu_counter_add(&sbinfo->used_blocks, -pages);
244 spin_lock(&inode->i_lock);
245 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
246 spin_unlock(&inode->i_lock);
250 static int shmem_reserve_inode(struct super_block *sb)
252 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
253 if (sbinfo->max_inodes) {
254 spin_lock(&sbinfo->stat_lock);
255 if (!sbinfo->free_inodes) {
256 spin_unlock(&sbinfo->stat_lock);
259 sbinfo->free_inodes--;
260 spin_unlock(&sbinfo->stat_lock);
265 static void shmem_free_inode(struct super_block *sb)
267 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
268 if (sbinfo->max_inodes) {
269 spin_lock(&sbinfo->stat_lock);
270 sbinfo->free_inodes++;
271 spin_unlock(&sbinfo->stat_lock);
276 * shmem_recalc_inode - recalculate the size of an inode
277 * @inode: inode to recalc
279 * We have to calculate the free blocks since the mm can drop
280 * undirtied hole pages behind our back.
282 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
283 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
285 * It has to be called with the spinlock held.
287 static void shmem_recalc_inode(struct inode *inode)
289 struct shmem_inode_info *info = SHMEM_I(inode);
292 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
294 info->alloced -= freed;
295 shmem_unacct_blocks(info->flags, freed);
296 shmem_free_blocks(inode, freed);
301 * shmem_swp_entry - find the swap vector position in the info structure
302 * @info: info structure for the inode
303 * @index: index of the page to find
304 * @page: optional page to add to the structure. Has to be preset to
307 * If there is no space allocated yet it will return NULL when
308 * page is NULL, else it will use the page for the needed block,
309 * setting it to NULL on return to indicate that it has been used.
311 * The swap vector is organized the following way:
313 * There are SHMEM_NR_DIRECT entries directly stored in the
314 * shmem_inode_info structure. So small files do not need an addional
317 * For pages with index > SHMEM_NR_DIRECT there is the pointer
318 * i_indirect which points to a page which holds in the first half
319 * doubly indirect blocks, in the second half triple indirect blocks:
321 * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
322 * following layout (for SHMEM_NR_DIRECT == 16):
324 * i_indirect -> dir --> 16-19
337 static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
339 unsigned long offset;
343 if (index < SHMEM_NR_DIRECT) {
344 shmem_swp_balance_unmap();
345 return info->i_direct+index;
347 if (!info->i_indirect) {
349 info->i_indirect = *page;
352 return NULL; /* need another page */
355 index -= SHMEM_NR_DIRECT;
356 offset = index % ENTRIES_PER_PAGE;
357 index /= ENTRIES_PER_PAGE;
358 dir = shmem_dir_map(info->i_indirect);
360 if (index >= ENTRIES_PER_PAGE/2) {
361 index -= ENTRIES_PER_PAGE/2;
362 dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
363 index %= ENTRIES_PER_PAGE;
370 shmem_dir_unmap(dir);
371 return NULL; /* need another page */
373 shmem_dir_unmap(dir);
374 dir = shmem_dir_map(subdir);
380 if (!page || !(subdir = *page)) {
381 shmem_dir_unmap(dir);
382 return NULL; /* need a page */
387 shmem_dir_unmap(dir);
388 return shmem_swp_map(subdir) + offset;
391 static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
393 long incdec = value? 1: -1;
396 info->swapped += incdec;
397 if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
398 struct page *page = kmap_atomic_to_page(entry);
399 set_page_private(page, page_private(page) + incdec);
404 * shmem_swp_alloc - get the position of the swap entry for the page.
405 * @info: info structure for the inode
406 * @index: index of the page to find
407 * @sgp: check and recheck i_size? skip allocation?
409 * If the entry does not exist, allocate it.
411 static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
413 struct inode *inode = &info->vfs_inode;
414 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
415 struct page *page = NULL;
418 if (sgp != SGP_WRITE &&
419 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
420 return ERR_PTR(-EINVAL);
422 while (!(entry = shmem_swp_entry(info, index, &page))) {
424 return shmem_swp_map(ZERO_PAGE(0));
426 * Test used_blocks against 1 less max_blocks, since we have 1 data
427 * page (and perhaps indirect index pages) yet to allocate:
428 * a waste to allocate index if we cannot allocate data.
430 if (sbinfo->max_blocks) {
431 if (percpu_counter_compare(&sbinfo->used_blocks,
432 sbinfo->max_blocks - 1) >= 0)
433 return ERR_PTR(-ENOSPC);
434 percpu_counter_inc(&sbinfo->used_blocks);
435 spin_lock(&inode->i_lock);
436 inode->i_blocks += BLOCKS_PER_PAGE;
437 spin_unlock(&inode->i_lock);
440 spin_unlock(&info->lock);
441 page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
442 spin_lock(&info->lock);
445 shmem_free_blocks(inode, 1);
446 return ERR_PTR(-ENOMEM);
448 if (sgp != SGP_WRITE &&
449 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
450 entry = ERR_PTR(-EINVAL);
453 if (info->next_index <= index)
454 info->next_index = index + 1;
457 /* another task gave its page, or truncated the file */
458 shmem_free_blocks(inode, 1);
459 shmem_dir_free(page);
461 if (info->next_index <= index && !IS_ERR(entry))
462 info->next_index = index + 1;
467 * shmem_free_swp - free some swap entries in a directory
468 * @dir: pointer to the directory
469 * @edir: pointer after last entry of the directory
470 * @punch_lock: pointer to spinlock when needed for the holepunch case
472 static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
473 spinlock_t *punch_lock)
475 spinlock_t *punch_unlock = NULL;
479 for (ptr = dir; ptr < edir; ptr++) {
481 if (unlikely(punch_lock)) {
482 punch_unlock = punch_lock;
484 spin_lock(punch_unlock);
488 free_swap_and_cache(*ptr);
489 *ptr = (swp_entry_t){0};
494 spin_unlock(punch_unlock);
498 static int shmem_map_and_free_swp(struct page *subdir, int offset,
499 int limit, struct page ***dir, spinlock_t *punch_lock)
504 ptr = shmem_swp_map(subdir);
505 for (; offset < limit; offset += LATENCY_LIMIT) {
506 int size = limit - offset;
507 if (size > LATENCY_LIMIT)
508 size = LATENCY_LIMIT;
509 freed += shmem_free_swp(ptr+offset, ptr+offset+size,
511 if (need_resched()) {
512 shmem_swp_unmap(ptr);
514 shmem_dir_unmap(*dir);
518 ptr = shmem_swp_map(subdir);
521 shmem_swp_unmap(ptr);
525 static void shmem_free_pages(struct list_head *next)
531 page = container_of(next, struct page, lru);
533 shmem_dir_free(page);
535 if (freed >= LATENCY_LIMIT) {
542 void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
544 struct shmem_inode_info *info = SHMEM_I(inode);
549 unsigned long diroff;
555 LIST_HEAD(pages_to_free);
556 long nr_pages_to_free = 0;
557 long nr_swaps_freed = 0;
561 spinlock_t *needs_lock;
562 spinlock_t *punch_lock;
563 unsigned long upper_limit;
565 truncate_inode_pages_range(inode->i_mapping, start, end);
567 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
568 idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
569 if (idx >= info->next_index)
572 spin_lock(&info->lock);
573 info->flags |= SHMEM_TRUNCATE;
574 if (likely(end == (loff_t) -1)) {
575 limit = info->next_index;
576 upper_limit = SHMEM_MAX_INDEX;
577 info->next_index = idx;
581 if (end + 1 >= inode->i_size) { /* we may free a little more */
582 limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
584 upper_limit = SHMEM_MAX_INDEX;
586 limit = (end + 1) >> PAGE_CACHE_SHIFT;
589 needs_lock = &info->lock;
593 topdir = info->i_indirect;
594 if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
595 info->i_indirect = NULL;
597 list_add(&topdir->lru, &pages_to_free);
599 spin_unlock(&info->lock);
601 if (info->swapped && idx < SHMEM_NR_DIRECT) {
602 ptr = info->i_direct;
604 if (size > SHMEM_NR_DIRECT)
605 size = SHMEM_NR_DIRECT;
606 nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
610 * If there are no indirect blocks or we are punching a hole
611 * below indirect blocks, nothing to be done.
613 if (!topdir || limit <= SHMEM_NR_DIRECT)
617 * The truncation case has already dropped info->lock, and we're safe
618 * because i_size and next_index have already been lowered, preventing
619 * access beyond. But in the punch_hole case, we still need to take
620 * the lock when updating the swap directory, because there might be
621 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
622 * shmem_writepage. However, whenever we find we can remove a whole
623 * directory page (not at the misaligned start or end of the range),
624 * we first NULLify its pointer in the level above, and then have no
625 * need to take the lock when updating its contents: needs_lock and
626 * punch_lock (either pointing to info->lock or NULL) manage this.
629 upper_limit -= SHMEM_NR_DIRECT;
630 limit -= SHMEM_NR_DIRECT;
631 idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
632 offset = idx % ENTRIES_PER_PAGE;
635 dir = shmem_dir_map(topdir);
636 stage = ENTRIES_PER_PAGEPAGE/2;
637 if (idx < ENTRIES_PER_PAGEPAGE/2) {
639 diroff = idx/ENTRIES_PER_PAGE;
641 dir += ENTRIES_PER_PAGE/2;
642 dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
644 stage += ENTRIES_PER_PAGEPAGE;
647 diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
648 ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
649 if (!diroff && !offset && upper_limit >= stage) {
651 spin_lock(needs_lock);
653 spin_unlock(needs_lock);
658 list_add(&middir->lru, &pages_to_free);
660 shmem_dir_unmap(dir);
661 dir = shmem_dir_map(middir);
669 for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
670 if (unlikely(idx == stage)) {
671 shmem_dir_unmap(dir);
672 dir = shmem_dir_map(topdir) +
673 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
676 idx += ENTRIES_PER_PAGEPAGE;
680 stage = idx + ENTRIES_PER_PAGEPAGE;
683 needs_lock = &info->lock;
684 if (upper_limit >= stage) {
686 spin_lock(needs_lock);
688 spin_unlock(needs_lock);
693 list_add(&middir->lru, &pages_to_free);
695 shmem_dir_unmap(dir);
697 dir = shmem_dir_map(middir);
700 punch_lock = needs_lock;
701 subdir = dir[diroff];
702 if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
704 spin_lock(needs_lock);
706 spin_unlock(needs_lock);
711 list_add(&subdir->lru, &pages_to_free);
713 if (subdir && page_private(subdir) /* has swap entries */) {
715 if (size > ENTRIES_PER_PAGE)
716 size = ENTRIES_PER_PAGE;
717 freed = shmem_map_and_free_swp(subdir,
718 offset, size, &dir, punch_lock);
720 dir = shmem_dir_map(middir);
721 nr_swaps_freed += freed;
722 if (offset || punch_lock) {
723 spin_lock(&info->lock);
724 set_page_private(subdir,
725 page_private(subdir) - freed);
726 spin_unlock(&info->lock);
728 BUG_ON(page_private(subdir) != freed);
733 shmem_dir_unmap(dir);
735 if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
737 * Call truncate_inode_pages again: racing shmem_unuse_inode
738 * may have swizzled a page in from swap since
739 * truncate_pagecache or generic_delete_inode did it, before we
740 * lowered next_index. Also, though shmem_getpage checks
741 * i_size before adding to cache, no recheck after: so fix the
742 * narrow window there too.
744 truncate_inode_pages_range(inode->i_mapping, start, end);
747 spin_lock(&info->lock);
748 info->flags &= ~SHMEM_TRUNCATE;
749 info->swapped -= nr_swaps_freed;
750 if (nr_pages_to_free)
751 shmem_free_blocks(inode, nr_pages_to_free);
752 shmem_recalc_inode(inode);
753 spin_unlock(&info->lock);
756 * Empty swap vector directory pages to be freed?
758 if (!list_empty(&pages_to_free)) {
759 pages_to_free.prev->next = NULL;
760 shmem_free_pages(pages_to_free.next);
763 EXPORT_SYMBOL_GPL(shmem_truncate_range);
765 static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
767 struct inode *inode = dentry->d_inode;
770 error = inode_change_ok(inode, attr);
774 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
775 loff_t oldsize = inode->i_size;
776 loff_t newsize = attr->ia_size;
777 struct page *page = NULL;
779 if (newsize < oldsize) {
781 * If truncating down to a partial page, then
782 * if that page is already allocated, hold it
783 * in memory until the truncation is over, so
784 * truncate_partial_page cannot miss it were
785 * it assigned to swap.
787 if (newsize & (PAGE_CACHE_SIZE-1)) {
788 (void) shmem_getpage(inode,
789 newsize >> PAGE_CACHE_SHIFT,
790 &page, SGP_READ, NULL);
795 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
796 * detect if any pages might have been added to cache
797 * after truncate_inode_pages. But we needn't bother
798 * if it's being fully truncated to zero-length: the
799 * nrpages check is efficient enough in that case.
802 struct shmem_inode_info *info = SHMEM_I(inode);
803 spin_lock(&info->lock);
804 info->flags &= ~SHMEM_PAGEIN;
805 spin_unlock(&info->lock);
808 if (newsize != oldsize) {
809 i_size_write(inode, newsize);
810 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
812 if (newsize < oldsize) {
813 loff_t holebegin = round_up(newsize, PAGE_SIZE);
814 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
815 shmem_truncate_range(inode, newsize, (loff_t)-1);
816 /* unmap again to remove racily COWed private pages */
817 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
820 page_cache_release(page);
823 setattr_copy(inode, attr);
824 #ifdef CONFIG_TMPFS_POSIX_ACL
825 if (attr->ia_valid & ATTR_MODE)
826 error = generic_acl_chmod(inode);
831 static void shmem_evict_inode(struct inode *inode)
833 struct shmem_inode_info *info = SHMEM_I(inode);
834 struct shmem_xattr *xattr, *nxattr;
836 if (inode->i_mapping->a_ops == &shmem_aops) {
837 shmem_unacct_size(info->flags, inode->i_size);
839 shmem_truncate_range(inode, 0, (loff_t)-1);
840 if (!list_empty(&info->swaplist)) {
841 mutex_lock(&shmem_swaplist_mutex);
842 list_del_init(&info->swaplist);
843 mutex_unlock(&shmem_swaplist_mutex);
847 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
851 BUG_ON(inode->i_blocks);
852 shmem_free_inode(inode->i_sb);
853 end_writeback(inode);
856 static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
860 for (ptr = dir; ptr < edir; ptr++) {
861 if (ptr->val == entry.val)
867 static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
869 struct address_space *mapping;
881 ptr = info->i_direct;
882 spin_lock(&info->lock);
883 if (!info->swapped) {
884 list_del_init(&info->swaplist);
887 limit = info->next_index;
889 if (size > SHMEM_NR_DIRECT)
890 size = SHMEM_NR_DIRECT;
891 offset = shmem_find_swp(entry, ptr, ptr+size);
893 shmem_swp_balance_unmap();
896 if (!info->i_indirect)
899 dir = shmem_dir_map(info->i_indirect);
900 stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
902 for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
903 if (unlikely(idx == stage)) {
904 shmem_dir_unmap(dir-1);
905 if (cond_resched_lock(&info->lock)) {
906 /* check it has not been truncated */
907 if (limit > info->next_index) {
908 limit = info->next_index;
913 dir = shmem_dir_map(info->i_indirect) +
914 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
917 idx += ENTRIES_PER_PAGEPAGE;
921 stage = idx + ENTRIES_PER_PAGEPAGE;
923 shmem_dir_unmap(dir);
924 dir = shmem_dir_map(subdir);
927 if (subdir && page_private(subdir)) {
928 ptr = shmem_swp_map(subdir);
930 if (size > ENTRIES_PER_PAGE)
931 size = ENTRIES_PER_PAGE;
932 offset = shmem_find_swp(entry, ptr, ptr+size);
933 shmem_swp_unmap(ptr);
935 shmem_dir_unmap(dir);
936 ptr = shmem_swp_map(subdir);
942 shmem_dir_unmap(dir-1);
944 spin_unlock(&info->lock);
951 * Move _head_ to start search for next from here.
952 * But be careful: shmem_evict_inode checks list_empty without taking
953 * mutex, and there's an instant in list_move_tail when info->swaplist
954 * would appear empty, if it were the only one on shmem_swaplist. We
955 * could avoid doing it if inode NULL; or use this minor optimization.
957 if (shmem_swaplist.next != &info->swaplist)
958 list_move_tail(&shmem_swaplist, &info->swaplist);
961 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
962 * but also to hold up shmem_evict_inode(): so inode cannot be freed
963 * beneath us (pagelock doesn't help until the page is in pagecache).
965 mapping = info->vfs_inode.i_mapping;
966 error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT);
967 /* which does mem_cgroup_uncharge_cache_page on error */
969 if (error == -EEXIST) {
970 struct page *filepage = find_get_page(mapping, idx);
974 * There might be a more uptodate page coming down
975 * from a stacked writepage: forget our swappage if so.
977 if (PageUptodate(filepage))
979 page_cache_release(filepage);
983 delete_from_swap_cache(page);
984 set_page_dirty(page);
985 info->flags |= SHMEM_PAGEIN;
986 shmem_swp_set(info, ptr, 0);
988 error = 1; /* not an error, but entry was found */
990 shmem_swp_unmap(ptr);
991 spin_unlock(&info->lock);
996 * shmem_unuse() search for an eventually swapped out shmem page.
998 int shmem_unuse(swp_entry_t entry, struct page *page)
1000 struct list_head *p, *next;
1001 struct shmem_inode_info *info;
1006 * Charge page using GFP_KERNEL while we can wait, before taking
1007 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1008 * Charged back to the user (not to caller) when swap account is used.
1009 * add_to_page_cache() will be called with GFP_NOWAIT.
1011 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
1015 * Try to preload while we can wait, to not make a habit of
1016 * draining atomic reserves; but don't latch on to this cpu,
1017 * it's okay if sometimes we get rescheduled after this.
1019 error = radix_tree_preload(GFP_KERNEL);
1022 radix_tree_preload_end();
1024 mutex_lock(&shmem_swaplist_mutex);
1025 list_for_each_safe(p, next, &shmem_swaplist) {
1026 info = list_entry(p, struct shmem_inode_info, swaplist);
1027 found = shmem_unuse_inode(info, entry, page);
1032 mutex_unlock(&shmem_swaplist_mutex);
1036 mem_cgroup_uncharge_cache_page(page);
1041 page_cache_release(page);
1046 * Move the page from the page cache to the swap cache.
1048 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1050 struct shmem_inode_info *info;
1051 swp_entry_t *entry, swap;
1052 struct address_space *mapping;
1053 unsigned long index;
1054 struct inode *inode;
1056 BUG_ON(!PageLocked(page));
1057 mapping = page->mapping;
1058 index = page->index;
1059 inode = mapping->host;
1060 info = SHMEM_I(inode);
1061 if (info->flags & VM_LOCKED)
1063 if (!total_swap_pages)
1067 * shmem_backing_dev_info's capabilities prevent regular writeback or
1068 * sync from ever calling shmem_writepage; but a stacking filesystem
1069 * may use the ->writepage of its underlying filesystem, in which case
1070 * tmpfs should write out to swap only in response to memory pressure,
1071 * and not for the writeback threads or sync. However, in those cases,
1072 * we do still want to check if there's a redundant swappage to be
1075 if (wbc->for_reclaim)
1076 swap = get_swap_page();
1081 * Add inode to shmem_unuse()'s list of swapped-out inodes,
1082 * if it's not already there. Do it now because we cannot take
1083 * mutex while holding spinlock, and must do so before the page
1084 * is moved to swap cache, when its pagelock no longer protects
1085 * the inode from eviction. But don't unlock the mutex until
1086 * we've taken the spinlock, because shmem_unuse_inode() will
1087 * prune a !swapped inode from the swaplist under both locks.
1090 mutex_lock(&shmem_swaplist_mutex);
1091 if (list_empty(&info->swaplist))
1092 list_add_tail(&info->swaplist, &shmem_swaplist);
1095 spin_lock(&info->lock);
1097 mutex_unlock(&shmem_swaplist_mutex);
1099 if (index >= info->next_index) {
1100 BUG_ON(!(info->flags & SHMEM_TRUNCATE));
1103 entry = shmem_swp_entry(info, index, NULL);
1106 * The more uptodate page coming down from a stacked
1107 * writepage should replace our old swappage.
1109 free_swap_and_cache(*entry);
1110 shmem_swp_set(info, entry, 0);
1112 shmem_recalc_inode(inode);
1114 if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
1115 delete_from_page_cache(page);
1116 shmem_swp_set(info, entry, swap.val);
1117 shmem_swp_unmap(entry);
1118 swap_shmem_alloc(swap);
1119 spin_unlock(&info->lock);
1120 BUG_ON(page_mapped(page));
1121 swap_writepage(page, wbc);
1125 shmem_swp_unmap(entry);
1127 spin_unlock(&info->lock);
1129 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
1130 * clear SWAP_HAS_CACHE flag.
1132 swapcache_free(swap, NULL);
1134 set_page_dirty(page);
1135 if (wbc->for_reclaim)
1136 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1143 static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1147 if (!mpol || mpol->mode == MPOL_DEFAULT)
1148 return; /* show nothing */
1150 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
1152 seq_printf(seq, ",mpol=%s", buffer);
1155 static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1157 struct mempolicy *mpol = NULL;
1159 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1160 mpol = sbinfo->mpol;
1162 spin_unlock(&sbinfo->stat_lock);
1166 #endif /* CONFIG_TMPFS */
1168 static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1169 struct shmem_inode_info *info, unsigned long idx)
1171 struct vm_area_struct pvma;
1174 /* Create a pseudo vma that just contains the policy */
1176 pvma.vm_pgoff = idx;
1178 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
1180 page = swapin_readahead(entry, gfp, &pvma, 0);
1182 /* Drop reference taken by mpol_shared_policy_lookup() */
1183 mpol_cond_put(pvma.vm_policy);
1188 static struct page *shmem_alloc_page(gfp_t gfp,
1189 struct shmem_inode_info *info, unsigned long idx)
1191 struct vm_area_struct pvma;
1194 /* Create a pseudo vma that just contains the policy */
1196 pvma.vm_pgoff = idx;
1198 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
1200 page = alloc_page_vma(gfp, &pvma, 0);
1202 /* Drop reference taken by mpol_shared_policy_lookup() */
1203 mpol_cond_put(pvma.vm_policy);
1207 #else /* !CONFIG_NUMA */
1209 static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
1212 #endif /* CONFIG_TMPFS */
1214 static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1215 struct shmem_inode_info *info, unsigned long idx)
1217 return swapin_readahead(entry, gfp, NULL, 0);
1220 static inline struct page *shmem_alloc_page(gfp_t gfp,
1221 struct shmem_inode_info *info, unsigned long idx)
1223 return alloc_page(gfp);
1225 #endif /* CONFIG_NUMA */
1227 #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
1228 static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1235 * shmem_getpage - either get the page from swap or allocate a new one
1237 * If we allocate a new one we do not mark it dirty. That's up to the
1238 * vm. If we swap it in we mark it dirty since we also free the swap
1239 * entry since a page cannot live in both the swap and page cache
1241 static int shmem_getpage(struct inode *inode, unsigned long idx,
1242 struct page **pagep, enum sgp_type sgp, int *type)
1244 struct address_space *mapping = inode->i_mapping;
1245 struct shmem_inode_info *info = SHMEM_I(inode);
1246 struct shmem_sb_info *sbinfo;
1247 struct page *filepage = *pagep;
1248 struct page *swappage;
1249 struct page *prealloc_page = NULL;
1255 if (idx >= SHMEM_MAX_INDEX)
1262 * Normally, filepage is NULL on entry, and either found
1263 * uptodate immediately, or allocated and zeroed, or read
1264 * in under swappage, which is then assigned to filepage.
1265 * But shmem_readpage (required for splice) passes in a locked
1266 * filepage, which may be found not uptodate by other callers
1267 * too, and may need to be copied from the swappage read in.
1271 filepage = find_lock_page(mapping, idx);
1272 if (filepage && PageUptodate(filepage))
1274 gfp = mapping_gfp_mask(mapping);
1277 * Try to preload while we can wait, to not make a habit of
1278 * draining atomic reserves; but don't latch on to this cpu.
1280 error = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
1283 radix_tree_preload_end();
1284 if (sgp != SGP_READ && !prealloc_page) {
1285 /* We don't care if this fails */
1286 prealloc_page = shmem_alloc_page(gfp, info, idx);
1287 if (prealloc_page) {
1288 if (mem_cgroup_cache_charge(prealloc_page,
1289 current->mm, GFP_KERNEL)) {
1290 page_cache_release(prealloc_page);
1291 prealloc_page = NULL;
1298 spin_lock(&info->lock);
1299 shmem_recalc_inode(inode);
1300 entry = shmem_swp_alloc(info, idx, sgp);
1301 if (IS_ERR(entry)) {
1302 spin_unlock(&info->lock);
1303 error = PTR_ERR(entry);
1309 /* Look it up and read it in.. */
1310 swappage = lookup_swap_cache(swap);
1312 shmem_swp_unmap(entry);
1313 spin_unlock(&info->lock);
1314 /* here we actually do the io */
1316 *type |= VM_FAULT_MAJOR;
1317 swappage = shmem_swapin(swap, gfp, info, idx);
1319 spin_lock(&info->lock);
1320 entry = shmem_swp_alloc(info, idx, sgp);
1322 error = PTR_ERR(entry);
1324 if (entry->val == swap.val)
1326 shmem_swp_unmap(entry);
1328 spin_unlock(&info->lock);
1333 wait_on_page_locked(swappage);
1334 page_cache_release(swappage);
1338 /* We have to do this with page locked to prevent races */
1339 if (!trylock_page(swappage)) {
1340 shmem_swp_unmap(entry);
1341 spin_unlock(&info->lock);
1342 wait_on_page_locked(swappage);
1343 page_cache_release(swappage);
1346 if (PageWriteback(swappage)) {
1347 shmem_swp_unmap(entry);
1348 spin_unlock(&info->lock);
1349 wait_on_page_writeback(swappage);
1350 unlock_page(swappage);
1351 page_cache_release(swappage);
1354 if (!PageUptodate(swappage)) {
1355 shmem_swp_unmap(entry);
1356 spin_unlock(&info->lock);
1357 unlock_page(swappage);
1358 page_cache_release(swappage);
1364 shmem_swp_set(info, entry, 0);
1365 shmem_swp_unmap(entry);
1366 delete_from_swap_cache(swappage);
1367 spin_unlock(&info->lock);
1368 copy_highpage(filepage, swappage);
1369 unlock_page(swappage);
1370 page_cache_release(swappage);
1371 flush_dcache_page(filepage);
1372 SetPageUptodate(filepage);
1373 set_page_dirty(filepage);
1375 } else if (!(error = add_to_page_cache_locked(swappage, mapping,
1376 idx, GFP_NOWAIT))) {
1377 info->flags |= SHMEM_PAGEIN;
1378 shmem_swp_set(info, entry, 0);
1379 shmem_swp_unmap(entry);
1380 delete_from_swap_cache(swappage);
1381 spin_unlock(&info->lock);
1382 filepage = swappage;
1383 set_page_dirty(filepage);
1386 shmem_swp_unmap(entry);
1387 spin_unlock(&info->lock);
1388 if (error == -ENOMEM) {
1390 * reclaim from proper memory cgroup and
1391 * call memcg's OOM if needed.
1393 error = mem_cgroup_shmem_charge_fallback(
1398 unlock_page(swappage);
1399 page_cache_release(swappage);
1403 unlock_page(swappage);
1404 page_cache_release(swappage);
1407 } else if (sgp == SGP_READ && !filepage) {
1408 shmem_swp_unmap(entry);
1409 filepage = find_get_page(mapping, idx);
1411 (!PageUptodate(filepage) || !trylock_page(filepage))) {
1412 spin_unlock(&info->lock);
1413 wait_on_page_locked(filepage);
1414 page_cache_release(filepage);
1418 spin_unlock(&info->lock);
1420 shmem_swp_unmap(entry);
1421 sbinfo = SHMEM_SB(inode->i_sb);
1422 if (sbinfo->max_blocks) {
1423 if (percpu_counter_compare(&sbinfo->used_blocks,
1424 sbinfo->max_blocks) >= 0 ||
1425 shmem_acct_block(info->flags))
1427 percpu_counter_inc(&sbinfo->used_blocks);
1428 spin_lock(&inode->i_lock);
1429 inode->i_blocks += BLOCKS_PER_PAGE;
1430 spin_unlock(&inode->i_lock);
1431 } else if (shmem_acct_block(info->flags))
1437 if (!prealloc_page) {
1438 spin_unlock(&info->lock);
1439 filepage = shmem_alloc_page(gfp, info, idx);
1441 shmem_unacct_blocks(info->flags, 1);
1442 shmem_free_blocks(inode, 1);
1446 SetPageSwapBacked(filepage);
1449 * Precharge page while we can wait, compensate
1452 error = mem_cgroup_cache_charge(filepage,
1453 current->mm, GFP_KERNEL);
1455 page_cache_release(filepage);
1456 shmem_unacct_blocks(info->flags, 1);
1457 shmem_free_blocks(inode, 1);
1462 spin_lock(&info->lock);
1464 filepage = prealloc_page;
1465 prealloc_page = NULL;
1466 SetPageSwapBacked(filepage);
1469 entry = shmem_swp_alloc(info, idx, sgp);
1471 error = PTR_ERR(entry);
1474 shmem_swp_unmap(entry);
1476 ret = error || swap.val;
1478 mem_cgroup_uncharge_cache_page(filepage);
1480 ret = add_to_page_cache_lru(filepage, mapping,
1483 * At add_to_page_cache_lru() failure, uncharge will
1484 * be done automatically.
1487 spin_unlock(&info->lock);
1488 page_cache_release(filepage);
1489 shmem_unacct_blocks(info->flags, 1);
1490 shmem_free_blocks(inode, 1);
1496 info->flags |= SHMEM_PAGEIN;
1500 spin_unlock(&info->lock);
1501 clear_highpage(filepage);
1502 flush_dcache_page(filepage);
1503 SetPageUptodate(filepage);
1504 if (sgp == SGP_DIRTY)
1505 set_page_dirty(filepage);
1514 * Perhaps the page was brought in from swap between find_lock_page
1515 * and taking info->lock? We allow for that at add_to_page_cache_lru,
1516 * but must also avoid reporting a spurious ENOSPC while working on a
1517 * full tmpfs. (When filepage has been passed in to shmem_getpage, it
1518 * is already in page cache, which prevents this race from occurring.)
1521 struct page *page = find_get_page(mapping, idx);
1523 spin_unlock(&info->lock);
1524 page_cache_release(page);
1528 spin_unlock(&info->lock);
1531 if (*pagep != filepage) {
1532 unlock_page(filepage);
1533 page_cache_release(filepage);
1536 if (prealloc_page) {
1537 mem_cgroup_uncharge_cache_page(prealloc_page);
1538 page_cache_release(prealloc_page);
1543 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1545 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1549 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1550 return VM_FAULT_SIGBUS;
1552 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1554 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1555 if (ret & VM_FAULT_MAJOR) {
1556 count_vm_event(PGMAJFAULT);
1557 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1559 return ret | VM_FAULT_LOCKED;
1563 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
1565 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1566 return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
1569 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1572 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1575 idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1576 return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
1580 int shmem_lock(struct file *file, int lock, struct user_struct *user)
1582 struct inode *inode = file->f_path.dentry->d_inode;
1583 struct shmem_inode_info *info = SHMEM_I(inode);
1584 int retval = -ENOMEM;
1586 spin_lock(&info->lock);
1587 if (lock && !(info->flags & VM_LOCKED)) {
1588 if (!user_shm_lock(inode->i_size, user))
1590 info->flags |= VM_LOCKED;
1591 mapping_set_unevictable(file->f_mapping);
1593 if (!lock && (info->flags & VM_LOCKED) && user) {
1594 user_shm_unlock(inode->i_size, user);
1595 info->flags &= ~VM_LOCKED;
1596 mapping_clear_unevictable(file->f_mapping);
1597 scan_mapping_unevictable_pages(file->f_mapping);
1602 spin_unlock(&info->lock);
1606 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1608 file_accessed(file);
1609 vma->vm_ops = &shmem_vm_ops;
1610 vma->vm_flags |= VM_CAN_NONLINEAR;
1614 static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1615 int mode, dev_t dev, unsigned long flags)
1617 struct inode *inode;
1618 struct shmem_inode_info *info;
1619 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1621 if (shmem_reserve_inode(sb))
1624 inode = new_inode(sb);
1626 inode->i_ino = get_next_ino();
1627 inode_init_owner(inode, dir, mode);
1628 inode->i_blocks = 0;
1629 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1630 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1631 inode->i_generation = get_seconds();
1632 info = SHMEM_I(inode);
1633 memset(info, 0, (char *)inode - (char *)info);
1634 spin_lock_init(&info->lock);
1635 info->flags = flags & VM_NORESERVE;
1636 INIT_LIST_HEAD(&info->swaplist);
1637 INIT_LIST_HEAD(&info->xattr_list);
1638 cache_no_acl(inode);
1640 switch (mode & S_IFMT) {
1642 inode->i_op = &shmem_special_inode_operations;
1643 init_special_inode(inode, mode, dev);
1646 inode->i_mapping->a_ops = &shmem_aops;
1647 inode->i_op = &shmem_inode_operations;
1648 inode->i_fop = &shmem_file_operations;
1649 mpol_shared_policy_init(&info->policy,
1650 shmem_get_sbmpol(sbinfo));
1654 /* Some things misbehave if size == 0 on a directory */
1655 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1656 inode->i_op = &shmem_dir_inode_operations;
1657 inode->i_fop = &simple_dir_operations;
1661 * Must not load anything in the rbtree,
1662 * mpol_free_shared_policy will not be called.
1664 mpol_shared_policy_init(&info->policy, NULL);
1668 shmem_free_inode(sb);
1673 static const struct inode_operations shmem_symlink_inode_operations;
1674 static const struct inode_operations shmem_symlink_inline_operations;
1677 * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
1678 * but providing them allows a tmpfs file to be used for splice, sendfile, and
1679 * below the loop driver, in the generic fashion that many filesystems support.
1681 static int shmem_readpage(struct file *file, struct page *page)
1683 struct inode *inode = page->mapping->host;
1684 int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
1690 shmem_write_begin(struct file *file, struct address_space *mapping,
1691 loff_t pos, unsigned len, unsigned flags,
1692 struct page **pagep, void **fsdata)
1694 struct inode *inode = mapping->host;
1695 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1697 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1701 shmem_write_end(struct file *file, struct address_space *mapping,
1702 loff_t pos, unsigned len, unsigned copied,
1703 struct page *page, void *fsdata)
1705 struct inode *inode = mapping->host;
1707 if (pos + copied > inode->i_size)
1708 i_size_write(inode, pos + copied);
1710 set_page_dirty(page);
1712 page_cache_release(page);
1717 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1719 struct inode *inode = filp->f_path.dentry->d_inode;
1720 struct address_space *mapping = inode->i_mapping;
1721 unsigned long index, offset;
1722 enum sgp_type sgp = SGP_READ;
1725 * Might this read be for a stacking filesystem? Then when reading
1726 * holes of a sparse file, we actually need to allocate those pages,
1727 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1729 if (segment_eq(get_fs(), KERNEL_DS))
1732 index = *ppos >> PAGE_CACHE_SHIFT;
1733 offset = *ppos & ~PAGE_CACHE_MASK;
1736 struct page *page = NULL;
1737 unsigned long end_index, nr, ret;
1738 loff_t i_size = i_size_read(inode);
1740 end_index = i_size >> PAGE_CACHE_SHIFT;
1741 if (index > end_index)
1743 if (index == end_index) {
1744 nr = i_size & ~PAGE_CACHE_MASK;
1749 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1751 if (desc->error == -EINVAL)
1759 * We must evaluate after, since reads (unlike writes)
1760 * are called without i_mutex protection against truncate
1762 nr = PAGE_CACHE_SIZE;
1763 i_size = i_size_read(inode);
1764 end_index = i_size >> PAGE_CACHE_SHIFT;
1765 if (index == end_index) {
1766 nr = i_size & ~PAGE_CACHE_MASK;
1769 page_cache_release(page);
1777 * If users can be writing to this page using arbitrary
1778 * virtual addresses, take care about potential aliasing
1779 * before reading the page on the kernel side.
1781 if (mapping_writably_mapped(mapping))
1782 flush_dcache_page(page);
1784 * Mark the page accessed if we read the beginning.
1787 mark_page_accessed(page);
1789 page = ZERO_PAGE(0);
1790 page_cache_get(page);
1794 * Ok, we have the page, and it's up-to-date, so
1795 * now we can copy it to user space...
1797 * The actor routine returns how many bytes were actually used..
1798 * NOTE! This may not be the same as how much of a user buffer
1799 * we filled up (we may be padding etc), so we can only update
1800 * "pos" here (the actor routine has to update the user buffer
1801 * pointers and the remaining count).
1803 ret = actor(desc, page, offset, nr);
1805 index += offset >> PAGE_CACHE_SHIFT;
1806 offset &= ~PAGE_CACHE_MASK;
1808 page_cache_release(page);
1809 if (ret != nr || !desc->count)
1815 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1816 file_accessed(filp);
1819 static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1820 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1822 struct file *filp = iocb->ki_filp;
1826 loff_t *ppos = &iocb->ki_pos;
1828 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1832 for (seg = 0; seg < nr_segs; seg++) {
1833 read_descriptor_t desc;
1836 desc.arg.buf = iov[seg].iov_base;
1837 desc.count = iov[seg].iov_len;
1838 if (desc.count == 0)
1841 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1842 retval += desc.written;
1844 retval = retval ?: desc.error;
1853 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1855 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1857 buf->f_type = TMPFS_MAGIC;
1858 buf->f_bsize = PAGE_CACHE_SIZE;
1859 buf->f_namelen = NAME_MAX;
1860 if (sbinfo->max_blocks) {
1861 buf->f_blocks = sbinfo->max_blocks;
1862 buf->f_bavail = buf->f_bfree =
1863 sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks);
1865 if (sbinfo->max_inodes) {
1866 buf->f_files = sbinfo->max_inodes;
1867 buf->f_ffree = sbinfo->free_inodes;
1869 /* else leave those fields 0 like simple_statfs */
1874 * File creation. Allocate an inode, and we're done..
1877 shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1879 struct inode *inode;
1880 int error = -ENOSPC;
1882 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1884 error = security_inode_init_security(inode, dir,
1885 &dentry->d_name, NULL,
1888 if (error != -EOPNOTSUPP) {
1893 #ifdef CONFIG_TMPFS_POSIX_ACL
1894 error = generic_acl_init(inode, dir);
1902 dir->i_size += BOGO_DIRENT_SIZE;
1903 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1904 d_instantiate(dentry, inode);
1905 dget(dentry); /* Extra count - pin the dentry in core */
1910 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1914 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1920 static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1921 struct nameidata *nd)
1923 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1929 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1931 struct inode *inode = old_dentry->d_inode;
1935 * No ordinary (disk based) filesystem counts links as inodes;
1936 * but each new link needs a new dentry, pinning lowmem, and
1937 * tmpfs dentries cannot be pruned until they are unlinked.
1939 ret = shmem_reserve_inode(inode->i_sb);
1943 dir->i_size += BOGO_DIRENT_SIZE;
1944 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1946 ihold(inode); /* New dentry reference */
1947 dget(dentry); /* Extra pinning count for the created dentry */
1948 d_instantiate(dentry, inode);
1953 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1955 struct inode *inode = dentry->d_inode;
1957 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1958 shmem_free_inode(inode->i_sb);
1960 dir->i_size -= BOGO_DIRENT_SIZE;
1961 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1963 dput(dentry); /* Undo the count from "create" - this does all the work */
1967 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1969 if (!simple_empty(dentry))
1972 drop_nlink(dentry->d_inode);
1974 return shmem_unlink(dir, dentry);
1978 * The VFS layer already does all the dentry stuff for rename,
1979 * we just have to decrement the usage count for the target if
1980 * it exists so that the VFS layer correctly free's it when it
1983 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1985 struct inode *inode = old_dentry->d_inode;
1986 int they_are_dirs = S_ISDIR(inode->i_mode);
1988 if (!simple_empty(new_dentry))
1991 if (new_dentry->d_inode) {
1992 (void) shmem_unlink(new_dir, new_dentry);
1994 drop_nlink(old_dir);
1995 } else if (they_are_dirs) {
1996 drop_nlink(old_dir);
2000 old_dir->i_size -= BOGO_DIRENT_SIZE;
2001 new_dir->i_size += BOGO_DIRENT_SIZE;
2002 old_dir->i_ctime = old_dir->i_mtime =
2003 new_dir->i_ctime = new_dir->i_mtime =
2004 inode->i_ctime = CURRENT_TIME;
2008 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2012 struct inode *inode;
2013 struct page *page = NULL;
2015 struct shmem_inode_info *info;
2017 len = strlen(symname) + 1;
2018 if (len > PAGE_CACHE_SIZE)
2019 return -ENAMETOOLONG;
2021 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
2025 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
2028 if (error != -EOPNOTSUPP) {
2035 info = SHMEM_I(inode);
2036 inode->i_size = len-1;
2037 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
2039 memcpy(info->inline_symlink, symname, len);
2040 inode->i_op = &shmem_symlink_inline_operations;
2042 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
2047 inode->i_mapping->a_ops = &shmem_aops;
2048 inode->i_op = &shmem_symlink_inode_operations;
2049 kaddr = kmap_atomic(page, KM_USER0);
2050 memcpy(kaddr, symname, len);
2051 kunmap_atomic(kaddr, KM_USER0);
2052 set_page_dirty(page);
2054 page_cache_release(page);
2056 dir->i_size += BOGO_DIRENT_SIZE;
2057 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2058 d_instantiate(dentry, inode);
2063 static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
2065 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
2069 static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
2071 struct page *page = NULL;
2072 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
2073 nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
2079 static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2081 if (!IS_ERR(nd_get_link(nd))) {
2082 struct page *page = cookie;
2084 mark_page_accessed(page);
2085 page_cache_release(page);
2089 #ifdef CONFIG_TMPFS_XATTR
2091 * Superblocks without xattr inode operations may get some security.* xattr
2092 * support from the LSM "for free". As soon as we have any other xattrs
2093 * like ACLs, we also need to implement the security.* handlers at
2094 * filesystem level, though.
2097 static int shmem_xattr_get(struct dentry *dentry, const char *name,
2098 void *buffer, size_t size)
2100 struct shmem_inode_info *info;
2101 struct shmem_xattr *xattr;
2104 info = SHMEM_I(dentry->d_inode);
2106 spin_lock(&info->lock);
2107 list_for_each_entry(xattr, &info->xattr_list, list) {
2108 if (strcmp(name, xattr->name))
2113 if (size < xattr->size)
2116 memcpy(buffer, xattr->value, xattr->size);
2120 spin_unlock(&info->lock);
2124 static int shmem_xattr_set(struct dentry *dentry, const char *name,
2125 const void *value, size_t size, int flags)
2127 struct inode *inode = dentry->d_inode;
2128 struct shmem_inode_info *info = SHMEM_I(inode);
2129 struct shmem_xattr *xattr;
2130 struct shmem_xattr *new_xattr = NULL;
2134 /* value == NULL means remove */
2137 len = sizeof(*new_xattr) + size;
2138 if (len <= sizeof(*new_xattr))
2141 new_xattr = kmalloc(len, GFP_KERNEL);
2145 new_xattr->name = kstrdup(name, GFP_KERNEL);
2146 if (!new_xattr->name) {
2151 new_xattr->size = size;
2152 memcpy(new_xattr->value, value, size);
2155 spin_lock(&info->lock);
2156 list_for_each_entry(xattr, &info->xattr_list, list) {
2157 if (!strcmp(name, xattr->name)) {
2158 if (flags & XATTR_CREATE) {
2161 } else if (new_xattr) {
2162 list_replace(&xattr->list, &new_xattr->list);
2164 list_del(&xattr->list);
2169 if (flags & XATTR_REPLACE) {
2173 list_add(&new_xattr->list, &info->xattr_list);
2177 spin_unlock(&info->lock);
2185 static const struct xattr_handler *shmem_xattr_handlers[] = {
2186 #ifdef CONFIG_TMPFS_POSIX_ACL
2187 &generic_acl_access_handler,
2188 &generic_acl_default_handler,
2193 static int shmem_xattr_validate(const char *name)
2195 struct { const char *prefix; size_t len; } arr[] = {
2196 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2197 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2201 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2202 size_t preflen = arr[i].len;
2203 if (strncmp(name, arr[i].prefix, preflen) == 0) {
2212 static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2213 void *buffer, size_t size)
2218 * If this is a request for a synthetic attribute in the system.*
2219 * namespace use the generic infrastructure to resolve a handler
2220 * for it via sb->s_xattr.
2222 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2223 return generic_getxattr(dentry, name, buffer, size);
2225 err = shmem_xattr_validate(name);
2229 return shmem_xattr_get(dentry, name, buffer, size);
2232 static int shmem_setxattr(struct dentry *dentry, const char *name,
2233 const void *value, size_t size, int flags)
2238 * If this is a request for a synthetic attribute in the system.*
2239 * namespace use the generic infrastructure to resolve a handler
2240 * for it via sb->s_xattr.
2242 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2243 return generic_setxattr(dentry, name, value, size, flags);
2245 err = shmem_xattr_validate(name);
2250 value = ""; /* empty EA, do not remove */
2252 return shmem_xattr_set(dentry, name, value, size, flags);
2256 static int shmem_removexattr(struct dentry *dentry, const char *name)
2261 * If this is a request for a synthetic attribute in the system.*
2262 * namespace use the generic infrastructure to resolve a handler
2263 * for it via sb->s_xattr.
2265 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2266 return generic_removexattr(dentry, name);
2268 err = shmem_xattr_validate(name);
2272 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
2275 static bool xattr_is_trusted(const char *name)
2277 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2280 static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2282 bool trusted = capable(CAP_SYS_ADMIN);
2283 struct shmem_xattr *xattr;
2284 struct shmem_inode_info *info;
2287 info = SHMEM_I(dentry->d_inode);
2289 spin_lock(&info->lock);
2290 list_for_each_entry(xattr, &info->xattr_list, list) {
2293 /* skip "trusted." attributes for unprivileged callers */
2294 if (!trusted && xattr_is_trusted(xattr->name))
2297 len = strlen(xattr->name) + 1;
2304 memcpy(buffer, xattr->name, len);
2308 spin_unlock(&info->lock);
2312 #endif /* CONFIG_TMPFS_XATTR */
2314 static const struct inode_operations shmem_symlink_inline_operations = {
2315 .readlink = generic_readlink,
2316 .follow_link = shmem_follow_link_inline,
2317 #ifdef CONFIG_TMPFS_XATTR
2318 .setxattr = shmem_setxattr,
2319 .getxattr = shmem_getxattr,
2320 .listxattr = shmem_listxattr,
2321 .removexattr = shmem_removexattr,
2325 static const struct inode_operations shmem_symlink_inode_operations = {
2326 .readlink = generic_readlink,
2327 .follow_link = shmem_follow_link,
2328 .put_link = shmem_put_link,
2329 #ifdef CONFIG_TMPFS_XATTR
2330 .setxattr = shmem_setxattr,
2331 .getxattr = shmem_getxattr,
2332 .listxattr = shmem_listxattr,
2333 .removexattr = shmem_removexattr,
2337 static struct dentry *shmem_get_parent(struct dentry *child)
2339 return ERR_PTR(-ESTALE);
2342 static int shmem_match(struct inode *ino, void *vfh)
2346 inum = (inum << 32) | fh[1];
2347 return ino->i_ino == inum && fh[0] == ino->i_generation;
2350 static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2351 struct fid *fid, int fh_len, int fh_type)
2353 struct inode *inode;
2354 struct dentry *dentry = NULL;
2361 inum = (inum << 32) | fid->raw[1];
2363 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2364 shmem_match, fid->raw);
2366 dentry = d_find_alias(inode);
2373 static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2376 struct inode *inode = dentry->d_inode;
2383 if (inode_unhashed(inode)) {
2384 /* Unfortunately insert_inode_hash is not idempotent,
2385 * so as we hash inodes here rather than at creation
2386 * time, we need a lock to ensure we only try
2389 static DEFINE_SPINLOCK(lock);
2391 if (inode_unhashed(inode))
2392 __insert_inode_hash(inode,
2393 inode->i_ino + inode->i_generation);
2397 fh[0] = inode->i_generation;
2398 fh[1] = inode->i_ino;
2399 fh[2] = ((__u64)inode->i_ino) >> 32;
2405 static const struct export_operations shmem_export_ops = {
2406 .get_parent = shmem_get_parent,
2407 .encode_fh = shmem_encode_fh,
2408 .fh_to_dentry = shmem_fh_to_dentry,
2411 static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2414 char *this_char, *value, *rest;
2416 while (options != NULL) {
2417 this_char = options;
2420 * NUL-terminate this option: unfortunately,
2421 * mount options form a comma-separated list,
2422 * but mpol's nodelist may also contain commas.
2424 options = strchr(options, ',');
2425 if (options == NULL)
2428 if (!isdigit(*options)) {
2435 if ((value = strchr(this_char,'=')) != NULL) {
2439 "tmpfs: No value for mount option '%s'\n",
2444 if (!strcmp(this_char,"size")) {
2445 unsigned long long size;
2446 size = memparse(value,&rest);
2448 size <<= PAGE_SHIFT;
2449 size *= totalram_pages;
2455 sbinfo->max_blocks =
2456 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
2457 } else if (!strcmp(this_char,"nr_blocks")) {
2458 sbinfo->max_blocks = memparse(value, &rest);
2461 } else if (!strcmp(this_char,"nr_inodes")) {
2462 sbinfo->max_inodes = memparse(value, &rest);
2465 } else if (!strcmp(this_char,"mode")) {
2468 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
2471 } else if (!strcmp(this_char,"uid")) {
2474 sbinfo->uid = simple_strtoul(value, &rest, 0);
2477 } else if (!strcmp(this_char,"gid")) {
2480 sbinfo->gid = simple_strtoul(value, &rest, 0);
2483 } else if (!strcmp(this_char,"mpol")) {
2484 if (mpol_parse_str(value, &sbinfo->mpol, 1))
2487 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2495 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2501 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2503 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2504 struct shmem_sb_info config = *sbinfo;
2505 unsigned long inodes;
2506 int error = -EINVAL;
2508 if (shmem_parse_options(data, &config, true))
2511 spin_lock(&sbinfo->stat_lock);
2512 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2513 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
2515 if (config.max_inodes < inodes)
2518 * Those tests also disallow limited->unlimited while any are in
2519 * use, so i_blocks will always be zero when max_blocks is zero;
2520 * but we must separately disallow unlimited->limited, because
2521 * in that case we have no record of how much is already in use.
2523 if (config.max_blocks && !sbinfo->max_blocks)
2525 if (config.max_inodes && !sbinfo->max_inodes)
2529 sbinfo->max_blocks = config.max_blocks;
2530 sbinfo->max_inodes = config.max_inodes;
2531 sbinfo->free_inodes = config.max_inodes - inodes;
2533 mpol_put(sbinfo->mpol);
2534 sbinfo->mpol = config.mpol; /* transfers initial ref */
2536 spin_unlock(&sbinfo->stat_lock);
2540 static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2542 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2544 if (sbinfo->max_blocks != shmem_default_max_blocks())
2545 seq_printf(seq, ",size=%luk",
2546 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2547 if (sbinfo->max_inodes != shmem_default_max_inodes())
2548 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2549 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2550 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2551 if (sbinfo->uid != 0)
2552 seq_printf(seq, ",uid=%u", sbinfo->uid);
2553 if (sbinfo->gid != 0)
2554 seq_printf(seq, ",gid=%u", sbinfo->gid);
2555 shmem_show_mpol(seq, sbinfo->mpol);
2558 #endif /* CONFIG_TMPFS */
2560 static void shmem_put_super(struct super_block *sb)
2562 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2564 percpu_counter_destroy(&sbinfo->used_blocks);
2566 sb->s_fs_info = NULL;
2569 int shmem_fill_super(struct super_block *sb, void *data, int silent)
2571 struct inode *inode;
2572 struct dentry *root;
2573 struct shmem_sb_info *sbinfo;
2576 /* Round up to L1_CACHE_BYTES to resist false sharing */
2577 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2578 L1_CACHE_BYTES), GFP_KERNEL);
2582 sbinfo->mode = S_IRWXUGO | S_ISVTX;
2583 sbinfo->uid = current_fsuid();
2584 sbinfo->gid = current_fsgid();
2585 sb->s_fs_info = sbinfo;
2589 * Per default we only allow half of the physical ram per
2590 * tmpfs instance, limiting inodes to one per page of lowmem;
2591 * but the internal instance is left unlimited.
2593 if (!(sb->s_flags & MS_NOUSER)) {
2594 sbinfo->max_blocks = shmem_default_max_blocks();
2595 sbinfo->max_inodes = shmem_default_max_inodes();
2596 if (shmem_parse_options(data, sbinfo, false)) {
2601 sb->s_export_op = &shmem_export_ops;
2603 sb->s_flags |= MS_NOUSER;
2606 spin_lock_init(&sbinfo->stat_lock);
2607 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2609 sbinfo->free_inodes = sbinfo->max_inodes;
2611 sb->s_maxbytes = SHMEM_MAX_BYTES;
2612 sb->s_blocksize = PAGE_CACHE_SIZE;
2613 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2614 sb->s_magic = TMPFS_MAGIC;
2615 sb->s_op = &shmem_ops;
2616 sb->s_time_gran = 1;
2617 #ifdef CONFIG_TMPFS_XATTR
2618 sb->s_xattr = shmem_xattr_handlers;
2620 #ifdef CONFIG_TMPFS_POSIX_ACL
2621 sb->s_flags |= MS_POSIXACL;
2624 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
2627 inode->i_uid = sbinfo->uid;
2628 inode->i_gid = sbinfo->gid;
2629 root = d_alloc_root(inode);
2638 shmem_put_super(sb);
2642 static struct kmem_cache *shmem_inode_cachep;
2644 static struct inode *shmem_alloc_inode(struct super_block *sb)
2646 struct shmem_inode_info *p;
2647 p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2650 return &p->vfs_inode;
2653 static void shmem_i_callback(struct rcu_head *head)
2655 struct inode *inode = container_of(head, struct inode, i_rcu);
2656 INIT_LIST_HEAD(&inode->i_dentry);
2657 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2660 static void shmem_destroy_inode(struct inode *inode)
2662 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2663 /* only struct inode is valid if it's an inline symlink */
2664 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2666 call_rcu(&inode->i_rcu, shmem_i_callback);
2669 static void init_once(void *foo)
2671 struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
2673 inode_init_once(&p->vfs_inode);
2676 static int init_inodecache(void)
2678 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2679 sizeof(struct shmem_inode_info),
2680 0, SLAB_PANIC, init_once);
2684 static void destroy_inodecache(void)
2686 kmem_cache_destroy(shmem_inode_cachep);
2689 static const struct address_space_operations shmem_aops = {
2690 .writepage = shmem_writepage,
2691 .set_page_dirty = __set_page_dirty_no_writeback,
2693 .readpage = shmem_readpage,
2694 .write_begin = shmem_write_begin,
2695 .write_end = shmem_write_end,
2697 .migratepage = migrate_page,
2698 .error_remove_page = generic_error_remove_page,
2701 static const struct file_operations shmem_file_operations = {
2704 .llseek = generic_file_llseek,
2705 .read = do_sync_read,
2706 .write = do_sync_write,
2707 .aio_read = shmem_file_aio_read,
2708 .aio_write = generic_file_aio_write,
2709 .fsync = noop_fsync,
2710 .splice_read = generic_file_splice_read,
2711 .splice_write = generic_file_splice_write,
2715 static const struct inode_operations shmem_inode_operations = {
2716 .setattr = shmem_setattr,
2717 .truncate_range = shmem_truncate_range,
2718 #ifdef CONFIG_TMPFS_XATTR
2719 .setxattr = shmem_setxattr,
2720 .getxattr = shmem_getxattr,
2721 .listxattr = shmem_listxattr,
2722 .removexattr = shmem_removexattr,
2724 #ifdef CONFIG_TMPFS_POSIX_ACL
2725 .check_acl = generic_check_acl,
2730 static const struct inode_operations shmem_dir_inode_operations = {
2732 .create = shmem_create,
2733 .lookup = simple_lookup,
2735 .unlink = shmem_unlink,
2736 .symlink = shmem_symlink,
2737 .mkdir = shmem_mkdir,
2738 .rmdir = shmem_rmdir,
2739 .mknod = shmem_mknod,
2740 .rename = shmem_rename,
2742 #ifdef CONFIG_TMPFS_XATTR
2743 .setxattr = shmem_setxattr,
2744 .getxattr = shmem_getxattr,
2745 .listxattr = shmem_listxattr,
2746 .removexattr = shmem_removexattr,
2748 #ifdef CONFIG_TMPFS_POSIX_ACL
2749 .setattr = shmem_setattr,
2750 .check_acl = generic_check_acl,
2754 static const struct inode_operations shmem_special_inode_operations = {
2755 #ifdef CONFIG_TMPFS_XATTR
2756 .setxattr = shmem_setxattr,
2757 .getxattr = shmem_getxattr,
2758 .listxattr = shmem_listxattr,
2759 .removexattr = shmem_removexattr,
2761 #ifdef CONFIG_TMPFS_POSIX_ACL
2762 .setattr = shmem_setattr,
2763 .check_acl = generic_check_acl,
2767 static const struct super_operations shmem_ops = {
2768 .alloc_inode = shmem_alloc_inode,
2769 .destroy_inode = shmem_destroy_inode,
2771 .statfs = shmem_statfs,
2772 .remount_fs = shmem_remount_fs,
2773 .show_options = shmem_show_options,
2775 .evict_inode = shmem_evict_inode,
2776 .drop_inode = generic_delete_inode,
2777 .put_super = shmem_put_super,
2780 static const struct vm_operations_struct shmem_vm_ops = {
2781 .fault = shmem_fault,
2783 .set_policy = shmem_set_policy,
2784 .get_policy = shmem_get_policy,
2789 static struct dentry *shmem_mount(struct file_system_type *fs_type,
2790 int flags, const char *dev_name, void *data)
2792 return mount_nodev(fs_type, flags, data, shmem_fill_super);
2795 static struct file_system_type tmpfs_fs_type = {
2796 .owner = THIS_MODULE,
2798 .mount = shmem_mount,
2799 .kill_sb = kill_litter_super,
2802 int __init init_tmpfs(void)
2806 error = bdi_init(&shmem_backing_dev_info);
2810 error = init_inodecache();
2814 error = register_filesystem(&tmpfs_fs_type);
2816 printk(KERN_ERR "Could not register tmpfs\n");
2820 shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
2821 tmpfs_fs_type.name, NULL);
2822 if (IS_ERR(shm_mnt)) {
2823 error = PTR_ERR(shm_mnt);
2824 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2830 unregister_filesystem(&tmpfs_fs_type);
2832 destroy_inodecache();
2834 bdi_destroy(&shmem_backing_dev_info);
2836 shm_mnt = ERR_PTR(error);
2840 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
2842 * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
2843 * @inode: the inode to be searched
2844 * @pgoff: the offset to be searched
2845 * @pagep: the pointer for the found page to be stored
2846 * @ent: the pointer for the found swap entry to be stored
2848 * If a page is found, refcount of it is incremented. Callers should handle
2851 void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
2852 struct page **pagep, swp_entry_t *ent)
2854 swp_entry_t entry = { .val = 0 }, *ptr;
2855 struct page *page = NULL;
2856 struct shmem_inode_info *info = SHMEM_I(inode);
2858 if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
2861 spin_lock(&info->lock);
2862 ptr = shmem_swp_entry(info, pgoff, NULL);
2864 if (ptr && ptr->val) {
2865 entry.val = ptr->val;
2866 page = find_get_page(&swapper_space, entry.val);
2869 page = find_get_page(inode->i_mapping, pgoff);
2871 shmem_swp_unmap(ptr);
2872 spin_unlock(&info->lock);
2879 #else /* !CONFIG_SHMEM */
2882 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2884 * This is intended for small system where the benefits of the full
2885 * shmem code (swap-backed and resource-limited) are outweighed by
2886 * their complexity. On systems without swap this code should be
2887 * effectively equivalent, but much lighter weight.
2890 #include <linux/ramfs.h>
2892 static struct file_system_type tmpfs_fs_type = {
2894 .mount = ramfs_mount,
2895 .kill_sb = kill_litter_super,
2898 int __init init_tmpfs(void)
2900 BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
2902 shm_mnt = kern_mount(&tmpfs_fs_type);
2903 BUG_ON(IS_ERR(shm_mnt));
2908 int shmem_unuse(swp_entry_t entry, struct page *page)
2913 int shmem_lock(struct file *file, int lock, struct user_struct *user)
2918 void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
2920 truncate_inode_pages_range(inode->i_mapping, start, end);
2922 EXPORT_SYMBOL_GPL(shmem_truncate_range);
2924 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
2926 * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
2927 * @inode: the inode to be searched
2928 * @pgoff: the offset to be searched
2929 * @pagep: the pointer for the found page to be stored
2930 * @ent: the pointer for the found swap entry to be stored
2932 * If a page is found, refcount of it is incremented. Callers should handle
2935 void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
2936 struct page **pagep, swp_entry_t *ent)
2938 struct page *page = NULL;
2940 if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
2942 page = find_get_page(inode->i_mapping, pgoff);
2945 *ent = (swp_entry_t){ .val = 0 };
2949 #define shmem_vm_ops generic_file_vm_ops
2950 #define shmem_file_operations ramfs_file_operations
2951 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
2952 #define shmem_acct_size(flags, size) 0
2953 #define shmem_unacct_size(flags, size) do {} while (0)
2954 #define SHMEM_MAX_BYTES MAX_LFS_FILESIZE
2956 #endif /* CONFIG_SHMEM */
2961 * shmem_file_setup - get an unlinked file living in tmpfs
2962 * @name: name for dentry (to be seen in /proc/<pid>/maps
2963 * @size: size to be set for the file
2964 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
2966 struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
2970 struct inode *inode;
2972 struct dentry *root;
2975 if (IS_ERR(shm_mnt))
2976 return (void *)shm_mnt;
2978 if (size < 0 || size > SHMEM_MAX_BYTES)
2979 return ERR_PTR(-EINVAL);
2981 if (shmem_acct_size(flags, size))
2982 return ERR_PTR(-ENOMEM);
2986 this.len = strlen(name);
2987 this.hash = 0; /* will go */
2988 root = shm_mnt->mnt_root;
2989 path.dentry = d_alloc(root, &this);
2992 path.mnt = mntget(shm_mnt);
2995 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
2999 d_instantiate(path.dentry, inode);
3000 inode->i_size = size;
3001 inode->i_nlink = 0; /* It is unlinked */
3003 error = ramfs_nommu_expand_for_mapping(inode, size);
3009 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
3010 &shmem_file_operations);
3019 shmem_unacct_size(flags, size);
3020 return ERR_PTR(error);
3022 EXPORT_SYMBOL_GPL(shmem_file_setup);
3024 void shmem_set_file(struct vm_area_struct *vma, struct file *file)
3028 vma->vm_file = file;
3029 vma->vm_ops = &shmem_vm_ops;
3030 vma->vm_flags |= VM_CAN_NONLINEAR;
3034 * shmem_zero_setup - setup a shared anonymous mapping
3035 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3037 int shmem_zero_setup(struct vm_area_struct *vma)
3040 loff_t size = vma->vm_end - vma->vm_start;
3042 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
3044 return PTR_ERR(file);
3046 shmem_set_file(vma, file);
3051 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3052 * @mapping: the page's address_space
3053 * @index: the page index
3054 * @gfp: the page allocator flags to use if allocating
3056 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3057 * with any new page allocations done using the specified allocation flags.
3058 * But read_cache_page_gfp() uses the ->readpage() method: which does not
3059 * suit tmpfs, since it may have pages in swapcache, and needs to find those
3060 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3062 * Provide a stub for those callers to start using now, then later
3063 * flesh it out to call shmem_getpage() with additional gfp mask, when
3064 * shmem_file_splice_read() is added and shmem_readpage() is removed.
3066 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3067 pgoff_t index, gfp_t gfp)
3069 return read_cache_page_gfp(mapping, index, gfp);
3071 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);