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>
17 * This file is released under the GPL.
21 * This virtual memory filesystem is heavily based on the ramfs. It
22 * extends ramfs by the ability to use swap and honor resource limits
23 * which makes it a completely usable filesystem.
26 #include <linux/module.h>
27 #include <linux/init.h>
29 #include <linux/xattr.h>
30 #include <linux/exportfs.h>
31 #include <linux/generic_acl.h>
33 #include <linux/mman.h>
34 #include <linux/file.h>
35 #include <linux/swap.h>
36 #include <linux/pagemap.h>
37 #include <linux/string.h>
38 #include <linux/slab.h>
39 #include <linux/backing-dev.h>
40 #include <linux/shmem_fs.h>
41 #include <linux/mount.h>
42 #include <linux/writeback.h>
43 #include <linux/vfs.h>
44 #include <linux/blkdev.h>
45 #include <linux/security.h>
46 #include <linux/swapops.h>
47 #include <linux/mempolicy.h>
48 #include <linux/namei.h>
49 #include <linux/ctype.h>
50 #include <linux/migrate.h>
51 #include <linux/highmem.h>
53 #include <asm/uaccess.h>
54 #include <asm/div64.h>
55 #include <asm/pgtable.h>
57 /* This magic number is used in glibc for posix shared memory */
58 #define TMPFS_MAGIC 0x01021994
60 #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
61 #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
62 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
64 #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
65 #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
67 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
69 /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
70 #define SHMEM_PAGEIN VM_READ
71 #define SHMEM_TRUNCATE VM_WRITE
73 /* Definition to limit shmem_truncate's steps between cond_rescheds */
74 #define LATENCY_LIMIT 64
76 /* Pretend that each entry is of this size in directory's i_size */
77 #define BOGO_DIRENT_SIZE 20
79 /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
81 SGP_READ, /* don't exceed i_size, don't allocate page */
82 SGP_CACHE, /* don't exceed i_size, may allocate page */
83 SGP_WRITE, /* may exceed i_size, may allocate page */
86 static int shmem_getpage(struct inode *inode, unsigned long idx,
87 struct page **pagep, enum sgp_type sgp, int *type);
89 static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
92 * The above definition of ENTRIES_PER_PAGE, and the use of
93 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
94 * might be reconsidered if it ever diverges from PAGE_SIZE.
96 * Mobility flags are masked out as swap vectors cannot move
98 return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
99 PAGE_CACHE_SHIFT-PAGE_SHIFT);
102 static inline void shmem_dir_free(struct page *page)
104 __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
107 static struct page **shmem_dir_map(struct page *page)
109 return (struct page **)kmap_atomic(page, KM_USER0);
112 static inline void shmem_dir_unmap(struct page **dir)
114 kunmap_atomic(dir, KM_USER0);
117 static swp_entry_t *shmem_swp_map(struct page *page)
119 return (swp_entry_t *)kmap_atomic(page, KM_USER1);
122 static inline void shmem_swp_balance_unmap(void)
125 * When passing a pointer to an i_direct entry, to code which
126 * also handles indirect entries and so will shmem_swp_unmap,
127 * we must arrange for the preempt count to remain in balance.
128 * What kmap_atomic of a lowmem page does depends on config
129 * and architecture, so pretend to kmap_atomic some lowmem page.
131 (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
134 static inline void shmem_swp_unmap(swp_entry_t *entry)
136 kunmap_atomic(entry, KM_USER1);
139 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
141 return sb->s_fs_info;
145 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
146 * for shared memory and for shared anonymous (/dev/zero) mappings
147 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
148 * consistent with the pre-accounting of private mappings ...
150 static inline int shmem_acct_size(unsigned long flags, loff_t size)
152 return (flags & VM_ACCOUNT)?
153 security_vm_enough_memory(VM_ACCT(size)): 0;
156 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
158 if (flags & VM_ACCOUNT)
159 vm_unacct_memory(VM_ACCT(size));
163 * ... whereas tmpfs objects are accounted incrementally as
164 * pages are allocated, in order to allow huge sparse files.
165 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
166 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
168 static inline int shmem_acct_block(unsigned long flags)
170 return (flags & VM_ACCOUNT)?
171 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
174 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
176 if (!(flags & VM_ACCOUNT))
177 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
180 static const struct super_operations shmem_ops;
181 static const struct address_space_operations shmem_aops;
182 static const struct file_operations shmem_file_operations;
183 static const struct inode_operations shmem_inode_operations;
184 static const struct inode_operations shmem_dir_inode_operations;
185 static const struct inode_operations shmem_special_inode_operations;
186 static struct vm_operations_struct shmem_vm_ops;
188 static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
189 .ra_pages = 0, /* No readahead */
190 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
191 .unplug_io_fn = default_unplug_io_fn,
194 static LIST_HEAD(shmem_swaplist);
195 static DEFINE_SPINLOCK(shmem_swaplist_lock);
197 static void shmem_free_blocks(struct inode *inode, long pages)
199 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
200 if (sbinfo->max_blocks) {
201 spin_lock(&sbinfo->stat_lock);
202 sbinfo->free_blocks += pages;
203 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
204 spin_unlock(&sbinfo->stat_lock);
209 * shmem_recalc_inode - recalculate the size of an inode
211 * @inode: inode to recalc
213 * We have to calculate the free blocks since the mm can drop
214 * undirtied hole pages behind our back.
216 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
217 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
219 * It has to be called with the spinlock held.
221 static void shmem_recalc_inode(struct inode *inode)
223 struct shmem_inode_info *info = SHMEM_I(inode);
226 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
228 info->alloced -= freed;
229 shmem_unacct_blocks(info->flags, freed);
230 shmem_free_blocks(inode, freed);
235 * shmem_swp_entry - find the swap vector position in the info structure
237 * @info: info structure for the inode
238 * @index: index of the page to find
239 * @page: optional page to add to the structure. Has to be preset to
242 * If there is no space allocated yet it will return NULL when
243 * page is NULL, else it will use the page for the needed block,
244 * setting it to NULL on return to indicate that it has been used.
246 * The swap vector is organized the following way:
248 * There are SHMEM_NR_DIRECT entries directly stored in the
249 * shmem_inode_info structure. So small files do not need an addional
252 * For pages with index > SHMEM_NR_DIRECT there is the pointer
253 * i_indirect which points to a page which holds in the first half
254 * doubly indirect blocks, in the second half triple indirect blocks:
256 * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
257 * following layout (for SHMEM_NR_DIRECT == 16):
259 * i_indirect -> dir --> 16-19
272 static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
274 unsigned long offset;
278 if (index < SHMEM_NR_DIRECT) {
279 shmem_swp_balance_unmap();
280 return info->i_direct+index;
282 if (!info->i_indirect) {
284 info->i_indirect = *page;
287 return NULL; /* need another page */
290 index -= SHMEM_NR_DIRECT;
291 offset = index % ENTRIES_PER_PAGE;
292 index /= ENTRIES_PER_PAGE;
293 dir = shmem_dir_map(info->i_indirect);
295 if (index >= ENTRIES_PER_PAGE/2) {
296 index -= ENTRIES_PER_PAGE/2;
297 dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
298 index %= ENTRIES_PER_PAGE;
305 shmem_dir_unmap(dir);
306 return NULL; /* need another page */
308 shmem_dir_unmap(dir);
309 dir = shmem_dir_map(subdir);
315 if (!page || !(subdir = *page)) {
316 shmem_dir_unmap(dir);
317 return NULL; /* need a page */
322 shmem_dir_unmap(dir);
323 return shmem_swp_map(subdir) + offset;
326 static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
328 long incdec = value? 1: -1;
331 info->swapped += incdec;
332 if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
333 struct page *page = kmap_atomic_to_page(entry);
334 set_page_private(page, page_private(page) + incdec);
339 * shmem_swp_alloc - get the position of the swap entry for the page.
340 * If it does not exist allocate the entry.
342 * @info: info structure for the inode
343 * @index: index of the page to find
344 * @sgp: check and recheck i_size? skip allocation?
346 static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
348 struct inode *inode = &info->vfs_inode;
349 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
350 struct page *page = NULL;
353 if (sgp != SGP_WRITE &&
354 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
355 return ERR_PTR(-EINVAL);
357 while (!(entry = shmem_swp_entry(info, index, &page))) {
359 return shmem_swp_map(ZERO_PAGE(0));
361 * Test free_blocks against 1 not 0, since we have 1 data
362 * page (and perhaps indirect index pages) yet to allocate:
363 * a waste to allocate index if we cannot allocate data.
365 if (sbinfo->max_blocks) {
366 spin_lock(&sbinfo->stat_lock);
367 if (sbinfo->free_blocks <= 1) {
368 spin_unlock(&sbinfo->stat_lock);
369 return ERR_PTR(-ENOSPC);
371 sbinfo->free_blocks--;
372 inode->i_blocks += BLOCKS_PER_PAGE;
373 spin_unlock(&sbinfo->stat_lock);
376 spin_unlock(&info->lock);
377 page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
379 set_page_private(page, 0);
380 spin_lock(&info->lock);
383 shmem_free_blocks(inode, 1);
384 return ERR_PTR(-ENOMEM);
386 if (sgp != SGP_WRITE &&
387 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
388 entry = ERR_PTR(-EINVAL);
391 if (info->next_index <= index)
392 info->next_index = index + 1;
395 /* another task gave its page, or truncated the file */
396 shmem_free_blocks(inode, 1);
397 shmem_dir_free(page);
399 if (info->next_index <= index && !IS_ERR(entry))
400 info->next_index = index + 1;
405 * shmem_free_swp - free some swap entries in a directory
407 * @dir: pointer to the directory
408 * @edir: pointer after last entry of the directory
409 * @punch_lock: pointer to spinlock when needed for the holepunch case
411 static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
412 spinlock_t *punch_lock)
414 spinlock_t *punch_unlock = NULL;
418 for (ptr = dir; ptr < edir; ptr++) {
420 if (unlikely(punch_lock)) {
421 punch_unlock = punch_lock;
423 spin_lock(punch_unlock);
427 free_swap_and_cache(*ptr);
428 *ptr = (swp_entry_t){0};
433 spin_unlock(punch_unlock);
437 static int shmem_map_and_free_swp(struct page *subdir, int offset,
438 int limit, struct page ***dir, spinlock_t *punch_lock)
443 ptr = shmem_swp_map(subdir);
444 for (; offset < limit; offset += LATENCY_LIMIT) {
445 int size = limit - offset;
446 if (size > LATENCY_LIMIT)
447 size = LATENCY_LIMIT;
448 freed += shmem_free_swp(ptr+offset, ptr+offset+size,
450 if (need_resched()) {
451 shmem_swp_unmap(ptr);
453 shmem_dir_unmap(*dir);
457 ptr = shmem_swp_map(subdir);
460 shmem_swp_unmap(ptr);
464 static void shmem_free_pages(struct list_head *next)
470 page = container_of(next, struct page, lru);
472 shmem_dir_free(page);
474 if (freed >= LATENCY_LIMIT) {
481 static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
483 struct shmem_inode_info *info = SHMEM_I(inode);
488 unsigned long diroff;
494 LIST_HEAD(pages_to_free);
495 long nr_pages_to_free = 0;
496 long nr_swaps_freed = 0;
500 spinlock_t *needs_lock;
501 spinlock_t *punch_lock;
502 unsigned long upper_limit;
504 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
505 idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
506 if (idx >= info->next_index)
509 spin_lock(&info->lock);
510 info->flags |= SHMEM_TRUNCATE;
511 if (likely(end == (loff_t) -1)) {
512 limit = info->next_index;
513 upper_limit = SHMEM_MAX_INDEX;
514 info->next_index = idx;
518 if (end + 1 >= inode->i_size) { /* we may free a little more */
519 limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
521 upper_limit = SHMEM_MAX_INDEX;
523 limit = (end + 1) >> PAGE_CACHE_SHIFT;
526 needs_lock = &info->lock;
530 topdir = info->i_indirect;
531 if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
532 info->i_indirect = NULL;
534 list_add(&topdir->lru, &pages_to_free);
536 spin_unlock(&info->lock);
538 if (info->swapped && idx < SHMEM_NR_DIRECT) {
539 ptr = info->i_direct;
541 if (size > SHMEM_NR_DIRECT)
542 size = SHMEM_NR_DIRECT;
543 nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
547 * If there are no indirect blocks or we are punching a hole
548 * below indirect blocks, nothing to be done.
550 if (!topdir || limit <= SHMEM_NR_DIRECT)
554 * The truncation case has already dropped info->lock, and we're safe
555 * because i_size and next_index have already been lowered, preventing
556 * access beyond. But in the punch_hole case, we still need to take
557 * the lock when updating the swap directory, because there might be
558 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
559 * shmem_writepage. However, whenever we find we can remove a whole
560 * directory page (not at the misaligned start or end of the range),
561 * we first NULLify its pointer in the level above, and then have no
562 * need to take the lock when updating its contents: needs_lock and
563 * punch_lock (either pointing to info->lock or NULL) manage this.
566 upper_limit -= SHMEM_NR_DIRECT;
567 limit -= SHMEM_NR_DIRECT;
568 idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
569 offset = idx % ENTRIES_PER_PAGE;
572 dir = shmem_dir_map(topdir);
573 stage = ENTRIES_PER_PAGEPAGE/2;
574 if (idx < ENTRIES_PER_PAGEPAGE/2) {
576 diroff = idx/ENTRIES_PER_PAGE;
578 dir += ENTRIES_PER_PAGE/2;
579 dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
581 stage += ENTRIES_PER_PAGEPAGE;
584 diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
585 ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
586 if (!diroff && !offset && upper_limit >= stage) {
588 spin_lock(needs_lock);
590 spin_unlock(needs_lock);
595 list_add(&middir->lru, &pages_to_free);
597 shmem_dir_unmap(dir);
598 dir = shmem_dir_map(middir);
606 for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
607 if (unlikely(idx == stage)) {
608 shmem_dir_unmap(dir);
609 dir = shmem_dir_map(topdir) +
610 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
613 idx += ENTRIES_PER_PAGEPAGE;
617 stage = idx + ENTRIES_PER_PAGEPAGE;
620 needs_lock = &info->lock;
621 if (upper_limit >= stage) {
623 spin_lock(needs_lock);
625 spin_unlock(needs_lock);
630 list_add(&middir->lru, &pages_to_free);
632 shmem_dir_unmap(dir);
634 dir = shmem_dir_map(middir);
637 punch_lock = needs_lock;
638 subdir = dir[diroff];
639 if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
641 spin_lock(needs_lock);
643 spin_unlock(needs_lock);
648 list_add(&subdir->lru, &pages_to_free);
650 if (subdir && page_private(subdir) /* has swap entries */) {
652 if (size > ENTRIES_PER_PAGE)
653 size = ENTRIES_PER_PAGE;
654 freed = shmem_map_and_free_swp(subdir,
655 offset, size, &dir, punch_lock);
657 dir = shmem_dir_map(middir);
658 nr_swaps_freed += freed;
659 if (offset || punch_lock) {
660 spin_lock(&info->lock);
661 set_page_private(subdir,
662 page_private(subdir) - freed);
663 spin_unlock(&info->lock);
665 BUG_ON(page_private(subdir) != freed);
670 shmem_dir_unmap(dir);
672 if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
674 * Call truncate_inode_pages again: racing shmem_unuse_inode
675 * may have swizzled a page in from swap since vmtruncate or
676 * generic_delete_inode did it, before we lowered next_index.
677 * Also, though shmem_getpage checks i_size before adding to
678 * cache, no recheck after: so fix the narrow window there too.
680 * Recalling truncate_inode_pages_range and unmap_mapping_range
681 * every time for punch_hole (which never got a chance to clear
682 * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive,
683 * yet hardly ever necessary: try to optimize them out later.
685 truncate_inode_pages_range(inode->i_mapping, start, end);
687 unmap_mapping_range(inode->i_mapping, start,
691 spin_lock(&info->lock);
692 info->flags &= ~SHMEM_TRUNCATE;
693 info->swapped -= nr_swaps_freed;
694 if (nr_pages_to_free)
695 shmem_free_blocks(inode, nr_pages_to_free);
696 shmem_recalc_inode(inode);
697 spin_unlock(&info->lock);
700 * Empty swap vector directory pages to be freed?
702 if (!list_empty(&pages_to_free)) {
703 pages_to_free.prev->next = NULL;
704 shmem_free_pages(pages_to_free.next);
708 static void shmem_truncate(struct inode *inode)
710 shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
713 static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
715 struct inode *inode = dentry->d_inode;
716 struct page *page = NULL;
719 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
720 if (attr->ia_size < inode->i_size) {
722 * If truncating down to a partial page, then
723 * if that page is already allocated, hold it
724 * in memory until the truncation is over, so
725 * truncate_partial_page cannnot miss it were
726 * it assigned to swap.
728 if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
729 (void) shmem_getpage(inode,
730 attr->ia_size>>PAGE_CACHE_SHIFT,
731 &page, SGP_READ, NULL);
734 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
735 * detect if any pages might have been added to cache
736 * after truncate_inode_pages. But we needn't bother
737 * if it's being fully truncated to zero-length: the
738 * nrpages check is efficient enough in that case.
741 struct shmem_inode_info *info = SHMEM_I(inode);
742 spin_lock(&info->lock);
743 info->flags &= ~SHMEM_PAGEIN;
744 spin_unlock(&info->lock);
749 error = inode_change_ok(inode, attr);
751 error = inode_setattr(inode, attr);
752 #ifdef CONFIG_TMPFS_POSIX_ACL
753 if (!error && (attr->ia_valid & ATTR_MODE))
754 error = generic_acl_chmod(inode, &shmem_acl_ops);
757 page_cache_release(page);
761 static void shmem_delete_inode(struct inode *inode)
763 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
764 struct shmem_inode_info *info = SHMEM_I(inode);
766 if (inode->i_op->truncate == shmem_truncate) {
767 truncate_inode_pages(inode->i_mapping, 0);
768 shmem_unacct_size(info->flags, inode->i_size);
770 shmem_truncate(inode);
771 if (!list_empty(&info->swaplist)) {
772 spin_lock(&shmem_swaplist_lock);
773 list_del_init(&info->swaplist);
774 spin_unlock(&shmem_swaplist_lock);
777 BUG_ON(inode->i_blocks);
778 if (sbinfo->max_inodes) {
779 spin_lock(&sbinfo->stat_lock);
780 sbinfo->free_inodes++;
781 spin_unlock(&sbinfo->stat_lock);
786 static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
790 for (ptr = dir; ptr < edir; ptr++) {
791 if (ptr->val == entry.val)
797 static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
810 ptr = info->i_direct;
811 spin_lock(&info->lock);
812 limit = info->next_index;
814 if (size > SHMEM_NR_DIRECT)
815 size = SHMEM_NR_DIRECT;
816 offset = shmem_find_swp(entry, ptr, ptr+size);
818 shmem_swp_balance_unmap();
821 if (!info->i_indirect)
824 dir = shmem_dir_map(info->i_indirect);
825 stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
827 for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
828 if (unlikely(idx == stage)) {
829 shmem_dir_unmap(dir-1);
830 dir = shmem_dir_map(info->i_indirect) +
831 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
834 idx += ENTRIES_PER_PAGEPAGE;
838 stage = idx + ENTRIES_PER_PAGEPAGE;
840 shmem_dir_unmap(dir);
841 dir = shmem_dir_map(subdir);
844 if (subdir && page_private(subdir)) {
845 ptr = shmem_swp_map(subdir);
847 if (size > ENTRIES_PER_PAGE)
848 size = ENTRIES_PER_PAGE;
849 offset = shmem_find_swp(entry, ptr, ptr+size);
851 shmem_dir_unmap(dir);
854 shmem_swp_unmap(ptr);
858 shmem_dir_unmap(dir-1);
860 spin_unlock(&info->lock);
864 inode = &info->vfs_inode;
865 if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
866 info->flags |= SHMEM_PAGEIN;
867 shmem_swp_set(info, ptr + offset, 0);
869 shmem_swp_unmap(ptr);
870 spin_unlock(&info->lock);
872 * Decrement swap count even when the entry is left behind:
873 * try_to_unuse will skip over mms, then reincrement count.
880 * shmem_unuse() search for an eventually swapped out shmem page.
882 int shmem_unuse(swp_entry_t entry, struct page *page)
884 struct list_head *p, *next;
885 struct shmem_inode_info *info;
888 spin_lock(&shmem_swaplist_lock);
889 list_for_each_safe(p, next, &shmem_swaplist) {
890 info = list_entry(p, struct shmem_inode_info, swaplist);
892 list_del_init(&info->swaplist);
893 else if (shmem_unuse_inode(info, entry, page)) {
894 /* move head to start search for next from here */
895 list_move_tail(&shmem_swaplist, &info->swaplist);
900 spin_unlock(&shmem_swaplist_lock);
905 * Move the page from the page cache to the swap cache.
907 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
909 struct shmem_inode_info *info;
910 swp_entry_t *entry, swap;
911 struct address_space *mapping;
915 BUG_ON(!PageLocked(page));
917 * shmem_backing_dev_info's capabilities prevent regular writeback or
918 * sync from ever calling shmem_writepage; but a stacking filesystem
919 * may use the ->writepage of its underlying filesystem, in which case
920 * we want to do nothing when that underlying filesystem is tmpfs
921 * (writing out to swap is useful as a response to memory pressure, but
922 * of no use to stabilize the data) - just redirty the page, unlock it
923 * and claim success in this case. AOP_WRITEPAGE_ACTIVATE, and the
924 * page_mapped check below, must be avoided unless we're in reclaim.
926 if (!wbc->for_reclaim) {
927 set_page_dirty(page);
931 BUG_ON(page_mapped(page));
933 mapping = page->mapping;
935 inode = mapping->host;
936 info = SHMEM_I(inode);
937 if (info->flags & VM_LOCKED)
939 swap = get_swap_page();
943 spin_lock(&info->lock);
944 shmem_recalc_inode(inode);
945 if (index >= info->next_index) {
946 BUG_ON(!(info->flags & SHMEM_TRUNCATE));
949 entry = shmem_swp_entry(info, index, NULL);
953 if (move_to_swap_cache(page, swap) == 0) {
954 shmem_swp_set(info, entry, swap.val);
955 shmem_swp_unmap(entry);
956 spin_unlock(&info->lock);
957 if (list_empty(&info->swaplist)) {
958 spin_lock(&shmem_swaplist_lock);
959 /* move instead of add in case we're racing */
960 list_move_tail(&info->swaplist, &shmem_swaplist);
961 spin_unlock(&shmem_swaplist_lock);
967 shmem_swp_unmap(entry);
969 spin_unlock(&info->lock);
972 set_page_dirty(page);
973 return AOP_WRITEPAGE_ACTIVATE; /* Return with the page locked */
977 static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
979 char *nodelist = strchr(value, ':');
983 /* NUL-terminate policy string */
985 if (nodelist_parse(nodelist, *policy_nodes))
987 if (!nodes_subset(*policy_nodes, node_states[N_HIGH_MEMORY]))
990 if (!strcmp(value, "default")) {
991 *policy = MPOL_DEFAULT;
992 /* Don't allow a nodelist */
995 } else if (!strcmp(value, "prefer")) {
996 *policy = MPOL_PREFERRED;
997 /* Insist on a nodelist of one node only */
999 char *rest = nodelist;
1000 while (isdigit(*rest))
1005 } else if (!strcmp(value, "bind")) {
1006 *policy = MPOL_BIND;
1007 /* Insist on a nodelist */
1010 } else if (!strcmp(value, "interleave")) {
1011 *policy = MPOL_INTERLEAVE;
1013 * Default to online nodes with memory if no nodelist
1016 *policy_nodes = node_states[N_HIGH_MEMORY];
1020 /* Restore string for error message */
1026 static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1027 struct shmem_inode_info *info, unsigned long idx)
1029 struct vm_area_struct pvma;
1032 /* Create a pseudo vma that just contains the policy */
1034 pvma.vm_pgoff = idx;
1036 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
1037 page = swapin_readahead(entry, gfp, &pvma, 0);
1038 mpol_free(pvma.vm_policy);
1042 static struct page *shmem_alloc_page(gfp_t gfp,
1043 struct shmem_inode_info *info, unsigned long idx)
1045 struct vm_area_struct pvma;
1048 /* Create a pseudo vma that just contains the policy */
1050 pvma.vm_pgoff = idx;
1052 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
1053 page = alloc_page_vma(gfp, &pvma, 0);
1054 mpol_free(pvma.vm_policy);
1058 static inline int shmem_parse_mpol(char *value, int *policy,
1059 nodemask_t *policy_nodes)
1064 static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1065 struct shmem_inode_info *info, unsigned long idx)
1067 return swapin_readahead(entry, gfp, NULL, 0);
1070 static inline struct page *shmem_alloc_page(gfp_t gfp,
1071 struct shmem_inode_info *info, unsigned long idx)
1073 return alloc_page(gfp);
1078 * shmem_getpage - either get the page from swap or allocate a new one
1080 * If we allocate a new one we do not mark it dirty. That's up to the
1081 * vm. If we swap it in we mark it dirty since we also free the swap
1082 * entry since a page cannot live in both the swap and page cache
1084 static int shmem_getpage(struct inode *inode, unsigned long idx,
1085 struct page **pagep, enum sgp_type sgp, int *type)
1087 struct address_space *mapping = inode->i_mapping;
1088 struct shmem_inode_info *info = SHMEM_I(inode);
1089 struct shmem_sb_info *sbinfo;
1090 struct page *filepage = *pagep;
1091 struct page *swappage;
1097 if (idx >= SHMEM_MAX_INDEX)
1104 * Normally, filepage is NULL on entry, and either found
1105 * uptodate immediately, or allocated and zeroed, or read
1106 * in under swappage, which is then assigned to filepage.
1107 * But shmem_readpage and shmem_write_begin pass in a locked
1108 * filepage, which may be found not uptodate by other callers
1109 * too, and may need to be copied from the swappage read in.
1113 filepage = find_lock_page(mapping, idx);
1114 if (filepage && PageUptodate(filepage))
1117 gfp = mapping_gfp_mask(mapping);
1119 spin_lock(&info->lock);
1120 shmem_recalc_inode(inode);
1121 entry = shmem_swp_alloc(info, idx, sgp);
1122 if (IS_ERR(entry)) {
1123 spin_unlock(&info->lock);
1124 error = PTR_ERR(entry);
1130 /* Look it up and read it in.. */
1131 swappage = lookup_swap_cache(swap);
1133 shmem_swp_unmap(entry);
1134 /* here we actually do the io */
1135 if (type && !(*type & VM_FAULT_MAJOR)) {
1136 __count_vm_event(PGMAJFAULT);
1137 *type |= VM_FAULT_MAJOR;
1139 spin_unlock(&info->lock);
1140 swappage = shmem_swapin(swap, gfp, info, idx);
1142 spin_lock(&info->lock);
1143 entry = shmem_swp_alloc(info, idx, sgp);
1145 error = PTR_ERR(entry);
1147 if (entry->val == swap.val)
1149 shmem_swp_unmap(entry);
1151 spin_unlock(&info->lock);
1156 wait_on_page_locked(swappage);
1157 page_cache_release(swappage);
1161 /* We have to do this with page locked to prevent races */
1162 if (TestSetPageLocked(swappage)) {
1163 shmem_swp_unmap(entry);
1164 spin_unlock(&info->lock);
1165 wait_on_page_locked(swappage);
1166 page_cache_release(swappage);
1169 if (PageWriteback(swappage)) {
1170 shmem_swp_unmap(entry);
1171 spin_unlock(&info->lock);
1172 wait_on_page_writeback(swappage);
1173 unlock_page(swappage);
1174 page_cache_release(swappage);
1177 if (!PageUptodate(swappage)) {
1178 shmem_swp_unmap(entry);
1179 spin_unlock(&info->lock);
1180 unlock_page(swappage);
1181 page_cache_release(swappage);
1187 shmem_swp_set(info, entry, 0);
1188 shmem_swp_unmap(entry);
1189 delete_from_swap_cache(swappage);
1190 spin_unlock(&info->lock);
1191 copy_highpage(filepage, swappage);
1192 unlock_page(swappage);
1193 page_cache_release(swappage);
1194 flush_dcache_page(filepage);
1195 SetPageUptodate(filepage);
1196 set_page_dirty(filepage);
1198 } else if (!(error = move_from_swap_cache(
1199 swappage, idx, mapping))) {
1200 info->flags |= SHMEM_PAGEIN;
1201 shmem_swp_set(info, entry, 0);
1202 shmem_swp_unmap(entry);
1203 spin_unlock(&info->lock);
1204 filepage = swappage;
1207 shmem_swp_unmap(entry);
1208 spin_unlock(&info->lock);
1209 unlock_page(swappage);
1210 page_cache_release(swappage);
1211 if (error == -ENOMEM) {
1212 /* let kswapd refresh zone for GFP_ATOMICs */
1213 congestion_wait(WRITE, HZ/50);
1217 } else if (sgp == SGP_READ && !filepage) {
1218 shmem_swp_unmap(entry);
1219 filepage = find_get_page(mapping, idx);
1221 (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
1222 spin_unlock(&info->lock);
1223 wait_on_page_locked(filepage);
1224 page_cache_release(filepage);
1228 spin_unlock(&info->lock);
1230 shmem_swp_unmap(entry);
1231 sbinfo = SHMEM_SB(inode->i_sb);
1232 if (sbinfo->max_blocks) {
1233 spin_lock(&sbinfo->stat_lock);
1234 if (sbinfo->free_blocks == 0 ||
1235 shmem_acct_block(info->flags)) {
1236 spin_unlock(&sbinfo->stat_lock);
1237 spin_unlock(&info->lock);
1241 sbinfo->free_blocks--;
1242 inode->i_blocks += BLOCKS_PER_PAGE;
1243 spin_unlock(&sbinfo->stat_lock);
1244 } else if (shmem_acct_block(info->flags)) {
1245 spin_unlock(&info->lock);
1251 spin_unlock(&info->lock);
1252 filepage = shmem_alloc_page(gfp, info, idx);
1254 shmem_unacct_blocks(info->flags, 1);
1255 shmem_free_blocks(inode, 1);
1260 spin_lock(&info->lock);
1261 entry = shmem_swp_alloc(info, idx, sgp);
1263 error = PTR_ERR(entry);
1266 shmem_swp_unmap(entry);
1268 if (error || swap.val || 0 != add_to_page_cache_lru(
1269 filepage, mapping, idx, GFP_ATOMIC)) {
1270 spin_unlock(&info->lock);
1271 page_cache_release(filepage);
1272 shmem_unacct_blocks(info->flags, 1);
1273 shmem_free_blocks(inode, 1);
1279 info->flags |= SHMEM_PAGEIN;
1283 spin_unlock(&info->lock);
1284 clear_highpage(filepage);
1285 flush_dcache_page(filepage);
1286 SetPageUptodate(filepage);
1289 if (*pagep != filepage) {
1291 if (sgp != SGP_CACHE)
1292 unlock_page(filepage);
1298 if (*pagep != filepage) {
1299 unlock_page(filepage);
1300 page_cache_release(filepage);
1305 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1307 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1311 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1312 return VM_FAULT_SIGBUS;
1314 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1316 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1318 mark_page_accessed(vmf->page);
1319 return ret | VM_FAULT_LOCKED;
1323 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
1325 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1326 return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
1329 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1332 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1335 idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1336 return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
1340 int shmem_lock(struct file *file, int lock, struct user_struct *user)
1342 struct inode *inode = file->f_path.dentry->d_inode;
1343 struct shmem_inode_info *info = SHMEM_I(inode);
1344 int retval = -ENOMEM;
1346 spin_lock(&info->lock);
1347 if (lock && !(info->flags & VM_LOCKED)) {
1348 if (!user_shm_lock(inode->i_size, user))
1350 info->flags |= VM_LOCKED;
1352 if (!lock && (info->flags & VM_LOCKED) && user) {
1353 user_shm_unlock(inode->i_size, user);
1354 info->flags &= ~VM_LOCKED;
1358 spin_unlock(&info->lock);
1362 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1364 file_accessed(file);
1365 vma->vm_ops = &shmem_vm_ops;
1366 vma->vm_flags |= VM_CAN_NONLINEAR;
1370 static struct inode *
1371 shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
1373 struct inode *inode;
1374 struct shmem_inode_info *info;
1375 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1377 if (sbinfo->max_inodes) {
1378 spin_lock(&sbinfo->stat_lock);
1379 if (!sbinfo->free_inodes) {
1380 spin_unlock(&sbinfo->stat_lock);
1383 sbinfo->free_inodes--;
1384 spin_unlock(&sbinfo->stat_lock);
1387 inode = new_inode(sb);
1389 inode->i_mode = mode;
1390 inode->i_uid = current->fsuid;
1391 inode->i_gid = current->fsgid;
1392 inode->i_blocks = 0;
1393 inode->i_mapping->a_ops = &shmem_aops;
1394 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1395 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1396 inode->i_generation = get_seconds();
1397 info = SHMEM_I(inode);
1398 memset(info, 0, (char *)inode - (char *)info);
1399 spin_lock_init(&info->lock);
1400 INIT_LIST_HEAD(&info->swaplist);
1402 switch (mode & S_IFMT) {
1404 inode->i_op = &shmem_special_inode_operations;
1405 init_special_inode(inode, mode, dev);
1408 inode->i_op = &shmem_inode_operations;
1409 inode->i_fop = &shmem_file_operations;
1410 mpol_shared_policy_init(&info->policy, sbinfo->policy,
1411 &sbinfo->policy_nodes);
1415 /* Some things misbehave if size == 0 on a directory */
1416 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1417 inode->i_op = &shmem_dir_inode_operations;
1418 inode->i_fop = &simple_dir_operations;
1422 * Must not load anything in the rbtree,
1423 * mpol_free_shared_policy will not be called.
1425 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
1429 } else if (sbinfo->max_inodes) {
1430 spin_lock(&sbinfo->stat_lock);
1431 sbinfo->free_inodes++;
1432 spin_unlock(&sbinfo->stat_lock);
1438 static const struct inode_operations shmem_symlink_inode_operations;
1439 static const struct inode_operations shmem_symlink_inline_operations;
1442 * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
1443 * but providing them allows a tmpfs file to be used for splice, sendfile, and
1444 * below the loop driver, in the generic fashion that many filesystems support.
1446 static int shmem_readpage(struct file *file, struct page *page)
1448 struct inode *inode = page->mapping->host;
1449 int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
1455 shmem_write_begin(struct file *file, struct address_space *mapping,
1456 loff_t pos, unsigned len, unsigned flags,
1457 struct page **pagep, void **fsdata)
1459 struct inode *inode = mapping->host;
1460 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1462 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1466 shmem_write_end(struct file *file, struct address_space *mapping,
1467 loff_t pos, unsigned len, unsigned copied,
1468 struct page *page, void *fsdata)
1470 struct inode *inode = mapping->host;
1472 set_page_dirty(page);
1473 page_cache_release(page);
1475 if (pos+copied > inode->i_size)
1476 i_size_write(inode, pos+copied);
1482 shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
1484 struct inode *inode = file->f_path.dentry->d_inode;
1486 unsigned long written;
1489 if ((ssize_t) count < 0)
1492 if (!access_ok(VERIFY_READ, buf, count))
1495 mutex_lock(&inode->i_mutex);
1500 err = generic_write_checks(file, &pos, &count, 0);
1504 err = remove_suid(file->f_path.dentry);
1508 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1511 struct page *page = NULL;
1512 unsigned long bytes, index, offset;
1516 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1517 index = pos >> PAGE_CACHE_SHIFT;
1518 bytes = PAGE_CACHE_SIZE - offset;
1523 * We don't hold page lock across copy from user -
1524 * what would it guard against? - so no deadlock here.
1525 * But it still may be a good idea to prefault below.
1528 err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
1533 if (PageHighMem(page)) {
1534 volatile unsigned char dummy;
1535 __get_user(dummy, buf);
1536 __get_user(dummy, buf + bytes - 1);
1538 kaddr = kmap_atomic(page, KM_USER0);
1539 left = __copy_from_user_inatomic(kaddr + offset,
1541 kunmap_atomic(kaddr, KM_USER0);
1545 left = __copy_from_user(kaddr + offset, buf, bytes);
1553 if (pos > inode->i_size)
1554 i_size_write(inode, pos);
1556 flush_dcache_page(page);
1557 set_page_dirty(page);
1558 mark_page_accessed(page);
1559 page_cache_release(page);
1569 * Our dirty pages are not counted in nr_dirty,
1570 * and we do not attempt to balance dirty pages.
1580 mutex_unlock(&inode->i_mutex);
1584 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1586 struct inode *inode = filp->f_path.dentry->d_inode;
1587 struct address_space *mapping = inode->i_mapping;
1588 unsigned long index, offset;
1590 index = *ppos >> PAGE_CACHE_SHIFT;
1591 offset = *ppos & ~PAGE_CACHE_MASK;
1594 struct page *page = NULL;
1595 unsigned long end_index, nr, ret;
1596 loff_t i_size = i_size_read(inode);
1598 end_index = i_size >> PAGE_CACHE_SHIFT;
1599 if (index > end_index)
1601 if (index == end_index) {
1602 nr = i_size & ~PAGE_CACHE_MASK;
1607 desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
1609 if (desc->error == -EINVAL)
1615 * We must evaluate after, since reads (unlike writes)
1616 * are called without i_mutex protection against truncate
1618 nr = PAGE_CACHE_SIZE;
1619 i_size = i_size_read(inode);
1620 end_index = i_size >> PAGE_CACHE_SHIFT;
1621 if (index == end_index) {
1622 nr = i_size & ~PAGE_CACHE_MASK;
1625 page_cache_release(page);
1633 * If users can be writing to this page using arbitrary
1634 * virtual addresses, take care about potential aliasing
1635 * before reading the page on the kernel side.
1637 if (mapping_writably_mapped(mapping))
1638 flush_dcache_page(page);
1640 * Mark the page accessed if we read the beginning.
1643 mark_page_accessed(page);
1645 page = ZERO_PAGE(0);
1646 page_cache_get(page);
1650 * Ok, we have the page, and it's up-to-date, so
1651 * now we can copy it to user space...
1653 * The actor routine returns how many bytes were actually used..
1654 * NOTE! This may not be the same as how much of a user buffer
1655 * we filled up (we may be padding etc), so we can only update
1656 * "pos" here (the actor routine has to update the user buffer
1657 * pointers and the remaining count).
1659 ret = actor(desc, page, offset, nr);
1661 index += offset >> PAGE_CACHE_SHIFT;
1662 offset &= ~PAGE_CACHE_MASK;
1664 page_cache_release(page);
1665 if (ret != nr || !desc->count)
1671 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1672 file_accessed(filp);
1675 static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1677 read_descriptor_t desc;
1679 if ((ssize_t) count < 0)
1681 if (!access_ok(VERIFY_WRITE, buf, count))
1691 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1693 return desc.written;
1697 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1699 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1701 buf->f_type = TMPFS_MAGIC;
1702 buf->f_bsize = PAGE_CACHE_SIZE;
1703 buf->f_namelen = NAME_MAX;
1704 spin_lock(&sbinfo->stat_lock);
1705 if (sbinfo->max_blocks) {
1706 buf->f_blocks = sbinfo->max_blocks;
1707 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
1709 if (sbinfo->max_inodes) {
1710 buf->f_files = sbinfo->max_inodes;
1711 buf->f_ffree = sbinfo->free_inodes;
1713 /* else leave those fields 0 like simple_statfs */
1714 spin_unlock(&sbinfo->stat_lock);
1719 * File creation. Allocate an inode, and we're done..
1722 shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1724 struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
1725 int error = -ENOSPC;
1728 error = security_inode_init_security(inode, dir, NULL, NULL,
1731 if (error != -EOPNOTSUPP) {
1736 error = shmem_acl_init(inode, dir);
1741 if (dir->i_mode & S_ISGID) {
1742 inode->i_gid = dir->i_gid;
1744 inode->i_mode |= S_ISGID;
1746 dir->i_size += BOGO_DIRENT_SIZE;
1747 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1748 d_instantiate(dentry, inode);
1749 dget(dentry); /* Extra count - pin the dentry in core */
1754 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1758 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1764 static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1765 struct nameidata *nd)
1767 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1773 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1775 struct inode *inode = old_dentry->d_inode;
1776 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1779 * No ordinary (disk based) filesystem counts links as inodes;
1780 * but each new link needs a new dentry, pinning lowmem, and
1781 * tmpfs dentries cannot be pruned until they are unlinked.
1783 if (sbinfo->max_inodes) {
1784 spin_lock(&sbinfo->stat_lock);
1785 if (!sbinfo->free_inodes) {
1786 spin_unlock(&sbinfo->stat_lock);
1789 sbinfo->free_inodes--;
1790 spin_unlock(&sbinfo->stat_lock);
1793 dir->i_size += BOGO_DIRENT_SIZE;
1794 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1796 atomic_inc(&inode->i_count); /* New dentry reference */
1797 dget(dentry); /* Extra pinning count for the created dentry */
1798 d_instantiate(dentry, inode);
1802 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1804 struct inode *inode = dentry->d_inode;
1806 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
1807 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1808 if (sbinfo->max_inodes) {
1809 spin_lock(&sbinfo->stat_lock);
1810 sbinfo->free_inodes++;
1811 spin_unlock(&sbinfo->stat_lock);
1815 dir->i_size -= BOGO_DIRENT_SIZE;
1816 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1818 dput(dentry); /* Undo the count from "create" - this does all the work */
1822 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1824 if (!simple_empty(dentry))
1827 drop_nlink(dentry->d_inode);
1829 return shmem_unlink(dir, dentry);
1833 * The VFS layer already does all the dentry stuff for rename,
1834 * we just have to decrement the usage count for the target if
1835 * it exists so that the VFS layer correctly free's it when it
1838 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1840 struct inode *inode = old_dentry->d_inode;
1841 int they_are_dirs = S_ISDIR(inode->i_mode);
1843 if (!simple_empty(new_dentry))
1846 if (new_dentry->d_inode) {
1847 (void) shmem_unlink(new_dir, new_dentry);
1849 drop_nlink(old_dir);
1850 } else if (they_are_dirs) {
1851 drop_nlink(old_dir);
1855 old_dir->i_size -= BOGO_DIRENT_SIZE;
1856 new_dir->i_size += BOGO_DIRENT_SIZE;
1857 old_dir->i_ctime = old_dir->i_mtime =
1858 new_dir->i_ctime = new_dir->i_mtime =
1859 inode->i_ctime = CURRENT_TIME;
1863 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1867 struct inode *inode;
1868 struct page *page = NULL;
1870 struct shmem_inode_info *info;
1872 len = strlen(symname) + 1;
1873 if (len > PAGE_CACHE_SIZE)
1874 return -ENAMETOOLONG;
1876 inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
1880 error = security_inode_init_security(inode, dir, NULL, NULL,
1883 if (error != -EOPNOTSUPP) {
1890 info = SHMEM_I(inode);
1891 inode->i_size = len-1;
1892 if (len <= (char *)inode - (char *)info) {
1894 memcpy(info, symname, len);
1895 inode->i_op = &shmem_symlink_inline_operations;
1897 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1902 inode->i_op = &shmem_symlink_inode_operations;
1903 kaddr = kmap_atomic(page, KM_USER0);
1904 memcpy(kaddr, symname, len);
1905 kunmap_atomic(kaddr, KM_USER0);
1906 set_page_dirty(page);
1907 page_cache_release(page);
1909 if (dir->i_mode & S_ISGID)
1910 inode->i_gid = dir->i_gid;
1911 dir->i_size += BOGO_DIRENT_SIZE;
1912 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1913 d_instantiate(dentry, inode);
1918 static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1920 nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
1924 static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1926 struct page *page = NULL;
1927 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1928 nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
1932 static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1934 if (!IS_ERR(nd_get_link(nd))) {
1935 struct page *page = cookie;
1937 mark_page_accessed(page);
1938 page_cache_release(page);
1942 static const struct inode_operations shmem_symlink_inline_operations = {
1943 .readlink = generic_readlink,
1944 .follow_link = shmem_follow_link_inline,
1947 static const struct inode_operations shmem_symlink_inode_operations = {
1948 .truncate = shmem_truncate,
1949 .readlink = generic_readlink,
1950 .follow_link = shmem_follow_link,
1951 .put_link = shmem_put_link,
1954 #ifdef CONFIG_TMPFS_POSIX_ACL
1956 * Superblocks without xattr inode operations will get security.* xattr
1957 * support from the VFS "for free". As soon as we have any other xattrs
1958 * like ACLs, we also need to implement the security.* handlers at
1959 * filesystem level, though.
1962 static size_t shmem_xattr_security_list(struct inode *inode, char *list,
1963 size_t list_len, const char *name,
1966 return security_inode_listsecurity(inode, list, list_len);
1969 static int shmem_xattr_security_get(struct inode *inode, const char *name,
1970 void *buffer, size_t size)
1972 if (strcmp(name, "") == 0)
1974 return security_inode_getsecurity(inode, name, buffer, size,
1978 static int shmem_xattr_security_set(struct inode *inode, const char *name,
1979 const void *value, size_t size, int flags)
1981 if (strcmp(name, "") == 0)
1983 return security_inode_setsecurity(inode, name, value, size, flags);
1986 static struct xattr_handler shmem_xattr_security_handler = {
1987 .prefix = XATTR_SECURITY_PREFIX,
1988 .list = shmem_xattr_security_list,
1989 .get = shmem_xattr_security_get,
1990 .set = shmem_xattr_security_set,
1993 static struct xattr_handler *shmem_xattr_handlers[] = {
1994 &shmem_xattr_acl_access_handler,
1995 &shmem_xattr_acl_default_handler,
1996 &shmem_xattr_security_handler,
2001 static struct dentry *shmem_get_parent(struct dentry *child)
2003 return ERR_PTR(-ESTALE);
2006 static int shmem_match(struct inode *ino, void *vfh)
2010 inum = (inum << 32) | fh[1];
2011 return ino->i_ino == inum && fh[0] == ino->i_generation;
2014 static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2015 struct fid *fid, int fh_len, int fh_type)
2017 struct inode *inode;
2018 struct dentry *dentry = NULL;
2019 u64 inum = fid->raw[2];
2020 inum = (inum << 32) | fid->raw[1];
2025 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2026 shmem_match, fid->raw);
2028 dentry = d_find_alias(inode);
2035 static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2038 struct inode *inode = dentry->d_inode;
2043 if (hlist_unhashed(&inode->i_hash)) {
2044 /* Unfortunately insert_inode_hash is not idempotent,
2045 * so as we hash inodes here rather than at creation
2046 * time, we need a lock to ensure we only try
2049 static DEFINE_SPINLOCK(lock);
2051 if (hlist_unhashed(&inode->i_hash))
2052 __insert_inode_hash(inode,
2053 inode->i_ino + inode->i_generation);
2057 fh[0] = inode->i_generation;
2058 fh[1] = inode->i_ino;
2059 fh[2] = ((__u64)inode->i_ino) >> 32;
2065 static const struct export_operations shmem_export_ops = {
2066 .get_parent = shmem_get_parent,
2067 .encode_fh = shmem_encode_fh,
2068 .fh_to_dentry = shmem_fh_to_dentry,
2071 static int shmem_parse_options(char *options, int *mode, uid_t *uid,
2072 gid_t *gid, unsigned long *blocks, unsigned long *inodes,
2073 int *policy, nodemask_t *policy_nodes)
2075 char *this_char, *value, *rest;
2077 while (options != NULL) {
2078 this_char = options;
2081 * NUL-terminate this option: unfortunately,
2082 * mount options form a comma-separated list,
2083 * but mpol's nodelist may also contain commas.
2085 options = strchr(options, ',');
2086 if (options == NULL)
2089 if (!isdigit(*options)) {
2096 if ((value = strchr(this_char,'=')) != NULL) {
2100 "tmpfs: No value for mount option '%s'\n",
2105 if (!strcmp(this_char,"size")) {
2106 unsigned long long size;
2107 size = memparse(value,&rest);
2109 size <<= PAGE_SHIFT;
2110 size *= totalram_pages;
2116 *blocks = size >> PAGE_CACHE_SHIFT;
2117 } else if (!strcmp(this_char,"nr_blocks")) {
2118 *blocks = memparse(value,&rest);
2121 } else if (!strcmp(this_char,"nr_inodes")) {
2122 *inodes = memparse(value,&rest);
2125 } else if (!strcmp(this_char,"mode")) {
2128 *mode = simple_strtoul(value,&rest,8);
2131 } else if (!strcmp(this_char,"uid")) {
2134 *uid = simple_strtoul(value,&rest,0);
2137 } else if (!strcmp(this_char,"gid")) {
2140 *gid = simple_strtoul(value,&rest,0);
2143 } else if (!strcmp(this_char,"mpol")) {
2144 if (shmem_parse_mpol(value,policy,policy_nodes))
2147 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2155 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2161 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2163 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2164 unsigned long max_blocks = sbinfo->max_blocks;
2165 unsigned long max_inodes = sbinfo->max_inodes;
2166 int policy = sbinfo->policy;
2167 nodemask_t policy_nodes = sbinfo->policy_nodes;
2168 unsigned long blocks;
2169 unsigned long inodes;
2170 int error = -EINVAL;
2172 if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
2173 &max_inodes, &policy, &policy_nodes))
2176 spin_lock(&sbinfo->stat_lock);
2177 blocks = sbinfo->max_blocks - sbinfo->free_blocks;
2178 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2179 if (max_blocks < blocks)
2181 if (max_inodes < inodes)
2184 * Those tests also disallow limited->unlimited while any are in
2185 * use, so i_blocks will always be zero when max_blocks is zero;
2186 * but we must separately disallow unlimited->limited, because
2187 * in that case we have no record of how much is already in use.
2189 if (max_blocks && !sbinfo->max_blocks)
2191 if (max_inodes && !sbinfo->max_inodes)
2195 sbinfo->max_blocks = max_blocks;
2196 sbinfo->free_blocks = max_blocks - blocks;
2197 sbinfo->max_inodes = max_inodes;
2198 sbinfo->free_inodes = max_inodes - inodes;
2199 sbinfo->policy = policy;
2200 sbinfo->policy_nodes = policy_nodes;
2202 spin_unlock(&sbinfo->stat_lock);
2207 static void shmem_put_super(struct super_block *sb)
2209 kfree(sb->s_fs_info);
2210 sb->s_fs_info = NULL;
2213 static int shmem_fill_super(struct super_block *sb,
2214 void *data, int silent)
2216 struct inode *inode;
2217 struct dentry *root;
2218 int mode = S_IRWXUGO | S_ISVTX;
2219 uid_t uid = current->fsuid;
2220 gid_t gid = current->fsgid;
2222 struct shmem_sb_info *sbinfo;
2223 unsigned long blocks = 0;
2224 unsigned long inodes = 0;
2225 int policy = MPOL_DEFAULT;
2226 nodemask_t policy_nodes = node_states[N_HIGH_MEMORY];
2230 * Per default we only allow half of the physical ram per
2231 * tmpfs instance, limiting inodes to one per page of lowmem;
2232 * but the internal instance is left unlimited.
2234 if (!(sb->s_flags & MS_NOUSER)) {
2235 blocks = totalram_pages / 2;
2236 inodes = totalram_pages - totalhigh_pages;
2237 if (inodes > blocks)
2239 if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
2240 &inodes, &policy, &policy_nodes))
2243 sb->s_export_op = &shmem_export_ops;
2245 sb->s_flags |= MS_NOUSER;
2248 /* Round up to L1_CACHE_BYTES to resist false sharing */
2249 sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
2250 L1_CACHE_BYTES), GFP_KERNEL);
2254 spin_lock_init(&sbinfo->stat_lock);
2255 sbinfo->max_blocks = blocks;
2256 sbinfo->free_blocks = blocks;
2257 sbinfo->max_inodes = inodes;
2258 sbinfo->free_inodes = inodes;
2259 sbinfo->policy = policy;
2260 sbinfo->policy_nodes = policy_nodes;
2262 sb->s_fs_info = sbinfo;
2263 sb->s_maxbytes = SHMEM_MAX_BYTES;
2264 sb->s_blocksize = PAGE_CACHE_SIZE;
2265 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2266 sb->s_magic = TMPFS_MAGIC;
2267 sb->s_op = &shmem_ops;
2268 sb->s_time_gran = 1;
2269 #ifdef CONFIG_TMPFS_POSIX_ACL
2270 sb->s_xattr = shmem_xattr_handlers;
2271 sb->s_flags |= MS_POSIXACL;
2274 inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
2279 root = d_alloc_root(inode);
2288 shmem_put_super(sb);
2292 static struct kmem_cache *shmem_inode_cachep;
2294 static struct inode *shmem_alloc_inode(struct super_block *sb)
2296 struct shmem_inode_info *p;
2297 p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2300 return &p->vfs_inode;
2303 static void shmem_destroy_inode(struct inode *inode)
2305 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2306 /* only struct inode is valid if it's an inline symlink */
2307 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2309 shmem_acl_destroy_inode(inode);
2310 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2313 static void init_once(struct kmem_cache *cachep, void *foo)
2315 struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
2317 inode_init_once(&p->vfs_inode);
2318 #ifdef CONFIG_TMPFS_POSIX_ACL
2320 p->i_default_acl = NULL;
2324 static int init_inodecache(void)
2326 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2327 sizeof(struct shmem_inode_info),
2328 0, SLAB_PANIC, init_once);
2332 static void destroy_inodecache(void)
2334 kmem_cache_destroy(shmem_inode_cachep);
2337 static const struct address_space_operations shmem_aops = {
2338 .writepage = shmem_writepage,
2339 .set_page_dirty = __set_page_dirty_no_writeback,
2341 .readpage = shmem_readpage,
2342 .write_begin = shmem_write_begin,
2343 .write_end = shmem_write_end,
2345 .migratepage = migrate_page,
2348 static const struct file_operations shmem_file_operations = {
2351 .llseek = generic_file_llseek,
2352 .read = shmem_file_read,
2353 .write = shmem_file_write,
2354 .fsync = simple_sync_file,
2355 .splice_read = generic_file_splice_read,
2356 .splice_write = generic_file_splice_write,
2360 static const struct inode_operations shmem_inode_operations = {
2361 .truncate = shmem_truncate,
2362 .setattr = shmem_notify_change,
2363 .truncate_range = shmem_truncate_range,
2364 #ifdef CONFIG_TMPFS_POSIX_ACL
2365 .setxattr = generic_setxattr,
2366 .getxattr = generic_getxattr,
2367 .listxattr = generic_listxattr,
2368 .removexattr = generic_removexattr,
2369 .permission = shmem_permission,
2374 static const struct inode_operations shmem_dir_inode_operations = {
2376 .create = shmem_create,
2377 .lookup = simple_lookup,
2379 .unlink = shmem_unlink,
2380 .symlink = shmem_symlink,
2381 .mkdir = shmem_mkdir,
2382 .rmdir = shmem_rmdir,
2383 .mknod = shmem_mknod,
2384 .rename = shmem_rename,
2386 #ifdef CONFIG_TMPFS_POSIX_ACL
2387 .setattr = shmem_notify_change,
2388 .setxattr = generic_setxattr,
2389 .getxattr = generic_getxattr,
2390 .listxattr = generic_listxattr,
2391 .removexattr = generic_removexattr,
2392 .permission = shmem_permission,
2396 static const struct inode_operations shmem_special_inode_operations = {
2397 #ifdef CONFIG_TMPFS_POSIX_ACL
2398 .setattr = shmem_notify_change,
2399 .setxattr = generic_setxattr,
2400 .getxattr = generic_getxattr,
2401 .listxattr = generic_listxattr,
2402 .removexattr = generic_removexattr,
2403 .permission = shmem_permission,
2407 static const struct super_operations shmem_ops = {
2408 .alloc_inode = shmem_alloc_inode,
2409 .destroy_inode = shmem_destroy_inode,
2411 .statfs = shmem_statfs,
2412 .remount_fs = shmem_remount_fs,
2414 .delete_inode = shmem_delete_inode,
2415 .drop_inode = generic_delete_inode,
2416 .put_super = shmem_put_super,
2419 static struct vm_operations_struct shmem_vm_ops = {
2420 .fault = shmem_fault,
2422 .set_policy = shmem_set_policy,
2423 .get_policy = shmem_get_policy,
2428 static int shmem_get_sb(struct file_system_type *fs_type,
2429 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2431 return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
2434 static struct file_system_type tmpfs_fs_type = {
2435 .owner = THIS_MODULE,
2437 .get_sb = shmem_get_sb,
2438 .kill_sb = kill_litter_super,
2440 static struct vfsmount *shm_mnt;
2442 static int __init init_tmpfs(void)
2446 error = bdi_init(&shmem_backing_dev_info);
2450 error = init_inodecache();
2454 error = register_filesystem(&tmpfs_fs_type);
2456 printk(KERN_ERR "Could not register tmpfs\n");
2460 shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
2461 tmpfs_fs_type.name, NULL);
2462 if (IS_ERR(shm_mnt)) {
2463 error = PTR_ERR(shm_mnt);
2464 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2470 unregister_filesystem(&tmpfs_fs_type);
2472 destroy_inodecache();
2474 bdi_destroy(&shmem_backing_dev_info);
2476 shm_mnt = ERR_PTR(error);
2479 module_init(init_tmpfs)
2482 * shmem_file_setup - get an unlinked file living in tmpfs
2484 * @name: name for dentry (to be seen in /proc/<pid>/maps
2485 * @size: size to be set for the file
2488 struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
2492 struct inode *inode;
2493 struct dentry *dentry, *root;
2496 if (IS_ERR(shm_mnt))
2497 return (void *)shm_mnt;
2499 if (size < 0 || size > SHMEM_MAX_BYTES)
2500 return ERR_PTR(-EINVAL);
2502 if (shmem_acct_size(flags, size))
2503 return ERR_PTR(-ENOMEM);
2507 this.len = strlen(name);
2508 this.hash = 0; /* will go */
2509 root = shm_mnt->mnt_root;
2510 dentry = d_alloc(root, &this);
2515 file = get_empty_filp();
2520 inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
2524 SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
2525 d_instantiate(dentry, inode);
2526 inode->i_size = size;
2527 inode->i_nlink = 0; /* It is unlinked */
2528 init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
2529 &shmem_file_operations);
2537 shmem_unacct_size(flags, size);
2538 return ERR_PTR(error);
2542 * shmem_zero_setup - setup a shared anonymous mapping
2544 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2546 int shmem_zero_setup(struct vm_area_struct *vma)
2549 loff_t size = vma->vm_end - vma->vm_start;
2551 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2553 return PTR_ERR(file);
2557 vma->vm_file = file;
2558 vma->vm_ops = &shmem_vm_ops;