2 * Resizable virtual memory filesystem for Linux.
4 * Copyright (C) 2000 Linus Torvalds.
6 * 2000-2001 Christoph Rohland
9 * Copyright (C) 2002-2003 Hugh Dickins.
10 * Copyright (C) 2002-2003 VERITAS Software Corporation.
12 * This file is released under the GPL.
16 * This virtual memory filesystem is heavily based on the ramfs. It
17 * extends ramfs by the ability to use swap and honor resource limits
18 * which makes it a completely usable filesystem.
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/devfs_fs_kernel.h>
27 #include <linux/mman.h>
28 #include <linux/file.h>
29 #include <linux/swap.h>
30 #include <linux/pagemap.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include <linux/backing-dev.h>
34 #include <linux/shmem_fs.h>
35 #include <linux/mount.h>
36 #include <linux/writeback.h>
37 #include <linux/vfs.h>
38 #include <linux/blkdev.h>
39 #include <linux/security.h>
40 #include <asm/uaccess.h>
41 #include <asm/div64.h>
43 /* This magic number is used in glibc for posix shared memory */
44 #define TMPFS_MAGIC 0x01021994
46 #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
47 #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
48 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
50 #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
51 #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
53 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
55 /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
56 #define SHMEM_PAGEIN VM_READ
57 #define SHMEM_TRUNCATE VM_WRITE
59 /* Pretend that each entry is of this size in directory's i_size */
60 #define BOGO_DIRENT_SIZE 20
62 /* Keep swapped page count in private field of indirect struct page */
63 #define nr_swapped private
65 /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
67 SGP_QUICK, /* don't try more than file page cache lookup */
68 SGP_READ, /* don't exceed i_size, don't allocate page */
69 SGP_CACHE, /* don't exceed i_size, may allocate page */
70 SGP_WRITE, /* may exceed i_size, may allocate page */
73 static int shmem_getpage(struct inode *inode, unsigned long idx,
74 struct page **pagep, enum sgp_type sgp, int *type);
76 static inline struct page *shmem_dir_alloc(unsigned int gfp_mask)
79 * The above definition of ENTRIES_PER_PAGE, and the use of
80 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
81 * might be reconsidered if it ever diverges from PAGE_SIZE.
83 return alloc_pages(gfp_mask, PAGE_CACHE_SHIFT-PAGE_SHIFT);
86 static inline void shmem_dir_free(struct page *page)
88 __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
91 static struct page **shmem_dir_map(struct page *page)
93 return (struct page **)kmap_atomic(page, KM_USER0);
96 static inline void shmem_dir_unmap(struct page **dir)
98 kunmap_atomic(dir, KM_USER0);
101 static swp_entry_t *shmem_swp_map(struct page *page)
104 * We have to avoid the unconditional inc_preempt_count()
105 * in kmap_atomic(), since shmem_swp_unmap() will also be
106 * applied to the low memory addresses within i_direct[].
107 * PageHighMem and high_memory tests are good for all arches
108 * and configs: highmem_start_page and FIXADDR_START are not.
110 return PageHighMem(page)?
111 (swp_entry_t *)kmap_atomic(page, KM_USER1):
112 (swp_entry_t *)page_address(page);
115 static inline void shmem_swp_unmap(swp_entry_t *entry)
117 if (entry >= (swp_entry_t *)high_memory)
118 kunmap_atomic(entry, KM_USER1);
121 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
123 return sb->s_fs_info;
127 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
128 * for shared memory and for shared anonymous (/dev/zero) mappings
129 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
130 * consistent with the pre-accounting of private mappings ...
132 static inline int shmem_acct_size(unsigned long flags, loff_t size)
134 return (flags & VM_ACCOUNT)?
135 security_vm_enough_memory(VM_ACCT(size)): 0;
138 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
140 if (flags & VM_ACCOUNT)
141 vm_unacct_memory(VM_ACCT(size));
145 * ... whereas tmpfs objects are accounted incrementally as
146 * pages are allocated, in order to allow huge sparse files.
147 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
148 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
150 static inline int shmem_acct_block(unsigned long flags)
152 return (flags & VM_ACCOUNT)?
153 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
156 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
158 if (!(flags & VM_ACCOUNT))
159 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
162 static struct super_operations shmem_ops;
163 static struct address_space_operations shmem_aops;
164 static struct file_operations shmem_file_operations;
165 static struct inode_operations shmem_inode_operations;
166 static struct inode_operations shmem_dir_inode_operations;
167 static struct vm_operations_struct shmem_vm_ops;
169 static struct backing_dev_info shmem_backing_dev_info = {
170 .ra_pages = 0, /* No readahead */
171 .memory_backed = 1, /* Does not contribute to dirty memory */
172 .unplug_io_fn = default_unplug_io_fn,
175 LIST_HEAD(shmem_inodes);
176 static spinlock_t shmem_ilock = SPIN_LOCK_UNLOCKED;
178 static void shmem_free_block(struct inode *inode)
180 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
181 spin_lock(&sbinfo->stat_lock);
182 sbinfo->free_blocks++;
183 inode->i_blocks -= BLOCKS_PER_PAGE;
184 spin_unlock(&sbinfo->stat_lock);
188 * shmem_recalc_inode - recalculate the size of an inode
190 * @inode: inode to recalc
192 * We have to calculate the free blocks since the mm can drop
193 * undirtied hole pages behind our back.
195 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
196 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
198 * It has to be called with the spinlock held.
200 static void shmem_recalc_inode(struct inode *inode)
202 struct shmem_inode_info *info = SHMEM_I(inode);
205 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
207 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
208 info->alloced -= freed;
209 spin_lock(&sbinfo->stat_lock);
210 sbinfo->free_blocks += freed;
211 inode->i_blocks -= freed*BLOCKS_PER_PAGE;
212 spin_unlock(&sbinfo->stat_lock);
213 shmem_unacct_blocks(info->flags, freed);
218 * shmem_swp_entry - find the swap vector position in the info structure
220 * @info: info structure for the inode
221 * @index: index of the page to find
222 * @page: optional page to add to the structure. Has to be preset to
225 * If there is no space allocated yet it will return NULL when
226 * page is NULL, else it will use the page for the needed block,
227 * setting it to NULL on return to indicate that it has been used.
229 * The swap vector is organized the following way:
231 * There are SHMEM_NR_DIRECT entries directly stored in the
232 * shmem_inode_info structure. So small files do not need an addional
235 * For pages with index > SHMEM_NR_DIRECT there is the pointer
236 * i_indirect which points to a page which holds in the first half
237 * doubly indirect blocks, in the second half triple indirect blocks:
239 * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
240 * following layout (for SHMEM_NR_DIRECT == 16):
242 * i_indirect -> dir --> 16-19
255 static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
257 unsigned long offset;
261 if (index < SHMEM_NR_DIRECT)
262 return info->i_direct+index;
263 if (!info->i_indirect) {
265 info->i_indirect = *page;
268 return NULL; /* need another page */
271 index -= SHMEM_NR_DIRECT;
272 offset = index % ENTRIES_PER_PAGE;
273 index /= ENTRIES_PER_PAGE;
274 dir = shmem_dir_map(info->i_indirect);
276 if (index >= ENTRIES_PER_PAGE/2) {
277 index -= ENTRIES_PER_PAGE/2;
278 dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
279 index %= ENTRIES_PER_PAGE;
286 shmem_dir_unmap(dir);
287 return NULL; /* need another page */
289 shmem_dir_unmap(dir);
290 dir = shmem_dir_map(subdir);
296 if (!page || !(subdir = *page)) {
297 shmem_dir_unmap(dir);
298 return NULL; /* need a page */
303 shmem_dir_unmap(dir);
306 * With apologies... caller shmem_swp_alloc passes non-NULL
307 * page (though perhaps NULL *page); and now we know that this
308 * indirect page has been allocated, we can shortcut the final
309 * kmap if we know it contains no swap entries, as is commonly
310 * the case: return pointer to a 0 which doesn't need kmapping.
312 return (page && !subdir->nr_swapped)?
313 (swp_entry_t *)&subdir->nr_swapped:
314 shmem_swp_map(subdir) + offset;
317 static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
319 long incdec = value? 1: -1;
322 info->swapped += incdec;
323 if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT)
324 kmap_atomic_to_page(entry)->nr_swapped += incdec;
328 * shmem_swp_alloc - get the position of the swap entry for the page.
329 * If it does not exist allocate the entry.
331 * @info: info structure for the inode
332 * @index: index of the page to find
333 * @sgp: check and recheck i_size? skip allocation?
335 static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
337 struct inode *inode = &info->vfs_inode;
338 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
339 struct page *page = NULL;
341 static const swp_entry_t unswapped = { 0 };
343 if (sgp != SGP_WRITE &&
344 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
345 return ERR_PTR(-EINVAL);
347 while (!(entry = shmem_swp_entry(info, index, &page))) {
349 return (swp_entry_t *) &unswapped;
351 * Test free_blocks against 1 not 0, since we have 1 data
352 * page (and perhaps indirect index pages) yet to allocate:
353 * a waste to allocate index if we cannot allocate data.
355 spin_lock(&sbinfo->stat_lock);
356 if (sbinfo->free_blocks <= 1) {
357 spin_unlock(&sbinfo->stat_lock);
358 return ERR_PTR(-ENOSPC);
360 sbinfo->free_blocks--;
361 inode->i_blocks += BLOCKS_PER_PAGE;
362 spin_unlock(&sbinfo->stat_lock);
364 spin_unlock(&info->lock);
365 page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
367 clear_highpage(page);
368 page->nr_swapped = 0;
370 spin_lock(&info->lock);
373 shmem_free_block(inode);
374 return ERR_PTR(-ENOMEM);
376 if (sgp != SGP_WRITE &&
377 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
378 entry = ERR_PTR(-EINVAL);
381 if (info->next_index <= index)
382 info->next_index = index + 1;
385 /* another task gave its page, or truncated the file */
386 shmem_free_block(inode);
387 shmem_dir_free(page);
389 if (info->next_index <= index && !IS_ERR(entry))
390 info->next_index = index + 1;
395 * shmem_free_swp - free some swap entries in a directory
397 * @dir: pointer to the directory
398 * @edir: pointer after last entry of the directory
400 static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir)
405 for (ptr = dir; ptr < edir; ptr++) {
407 free_swap_and_cache(*ptr);
408 *ptr = (swp_entry_t){0};
415 static void shmem_truncate(struct inode *inode)
417 struct shmem_inode_info *info = SHMEM_I(inode);
429 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
430 idx = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
431 if (idx >= info->next_index)
434 spin_lock(&info->lock);
435 info->flags |= SHMEM_TRUNCATE;
436 limit = info->next_index;
437 info->next_index = idx;
438 if (info->swapped && idx < SHMEM_NR_DIRECT) {
439 ptr = info->i_direct;
441 if (size > SHMEM_NR_DIRECT)
442 size = SHMEM_NR_DIRECT;
443 info->swapped -= shmem_free_swp(ptr+idx, ptr+size);
445 if (!info->i_indirect)
448 BUG_ON(limit <= SHMEM_NR_DIRECT);
449 limit -= SHMEM_NR_DIRECT;
450 idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
451 offset = idx % ENTRIES_PER_PAGE;
455 dir = shmem_dir_map(info->i_indirect);
456 stage = ENTRIES_PER_PAGEPAGE/2;
457 if (idx < ENTRIES_PER_PAGEPAGE/2)
458 dir += idx/ENTRIES_PER_PAGE;
460 dir += ENTRIES_PER_PAGE/2;
461 dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
463 stage += ENTRIES_PER_PAGEPAGE;
466 size = ((idx - ENTRIES_PER_PAGEPAGE/2) %
467 ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
468 if (!size && !offset) {
472 shmem_dir_unmap(dir);
473 dir = shmem_dir_map(subdir) + size;
480 for (; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
481 if (unlikely(idx == stage)) {
482 shmem_dir_unmap(dir-1);
483 dir = shmem_dir_map(info->i_indirect) +
484 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
487 idx += ENTRIES_PER_PAGEPAGE;
491 stage = idx + ENTRIES_PER_PAGEPAGE;
494 shmem_dir_unmap(dir);
496 shmem_dir_free(empty);
497 shmem_free_block(inode);
500 cond_resched_lock(&info->lock);
501 dir = shmem_dir_map(subdir);
504 if (subdir && subdir->nr_swapped) {
505 ptr = shmem_swp_map(subdir);
507 if (size > ENTRIES_PER_PAGE)
508 size = ENTRIES_PER_PAGE;
509 freed = shmem_free_swp(ptr+offset, ptr+size);
510 shmem_swp_unmap(ptr);
511 info->swapped -= freed;
512 subdir->nr_swapped -= freed;
513 BUG_ON(subdir->nr_swapped > offset);
519 shmem_dir_free(subdir);
520 shmem_free_block(inode);
524 shmem_dir_unmap(dir-1);
526 shmem_dir_free(empty);
527 shmem_free_block(inode);
529 if (info->next_index <= SHMEM_NR_DIRECT) {
530 shmem_dir_free(info->i_indirect);
531 info->i_indirect = NULL;
532 shmem_free_block(inode);
535 BUG_ON(info->swapped > info->next_index);
536 if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
538 * Call truncate_inode_pages again: racing shmem_unuse_inode
539 * may have swizzled a page in from swap since vmtruncate or
540 * generic_delete_inode did it, before we lowered next_index.
541 * Also, though shmem_getpage checks i_size before adding to
542 * cache, no recheck after: so fix the narrow window there too.
544 spin_unlock(&info->lock);
545 truncate_inode_pages(inode->i_mapping, inode->i_size);
546 spin_lock(&info->lock);
548 info->flags &= ~SHMEM_TRUNCATE;
549 shmem_recalc_inode(inode);
550 spin_unlock(&info->lock);
553 static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
555 struct inode *inode = dentry->d_inode;
556 struct page *page = NULL;
559 if (attr->ia_valid & ATTR_SIZE) {
560 if (attr->ia_size < inode->i_size) {
562 * If truncating down to a partial page, then
563 * if that page is already allocated, hold it
564 * in memory until the truncation is over, so
565 * truncate_partial_page cannnot miss it were
566 * it assigned to swap.
568 if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
569 (void) shmem_getpage(inode,
570 attr->ia_size>>PAGE_CACHE_SHIFT,
571 &page, SGP_READ, NULL);
574 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
575 * detect if any pages might have been added to cache
576 * after truncate_inode_pages. But we needn't bother
577 * if it's being fully truncated to zero-length: the
578 * nrpages check is efficient enough in that case.
581 struct shmem_inode_info *info = SHMEM_I(inode);
582 spin_lock(&info->lock);
583 info->flags &= ~SHMEM_PAGEIN;
584 spin_unlock(&info->lock);
589 error = inode_change_ok(inode, attr);
591 error = inode_setattr(inode, attr);
593 page_cache_release(page);
597 static void shmem_delete_inode(struct inode *inode)
599 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
600 struct shmem_inode_info *info = SHMEM_I(inode);
602 if (inode->i_op->truncate == shmem_truncate) {
603 spin_lock(&shmem_ilock);
604 list_del(&info->list);
605 spin_unlock(&shmem_ilock);
606 shmem_unacct_size(info->flags, inode->i_size);
608 shmem_truncate(inode);
610 BUG_ON(inode->i_blocks);
611 spin_lock(&sbinfo->stat_lock);
612 sbinfo->free_inodes++;
613 spin_unlock(&sbinfo->stat_lock);
617 static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
621 for (ptr = dir; ptr < edir; ptr++) {
622 if (ptr->val == entry.val)
628 static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
641 ptr = info->i_direct;
642 spin_lock(&info->lock);
643 limit = info->next_index;
645 if (size > SHMEM_NR_DIRECT)
646 size = SHMEM_NR_DIRECT;
647 offset = shmem_find_swp(entry, ptr, ptr+size);
650 if (!info->i_indirect)
652 /* we might be racing with shmem_truncate */
653 if (limit <= SHMEM_NR_DIRECT)
656 dir = shmem_dir_map(info->i_indirect);
657 stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
659 for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
660 if (unlikely(idx == stage)) {
661 shmem_dir_unmap(dir-1);
662 dir = shmem_dir_map(info->i_indirect) +
663 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
666 idx += ENTRIES_PER_PAGEPAGE;
670 stage = idx + ENTRIES_PER_PAGEPAGE;
672 shmem_dir_unmap(dir);
673 dir = shmem_dir_map(subdir);
676 if (subdir && subdir->nr_swapped) {
677 ptr = shmem_swp_map(subdir);
679 if (size > ENTRIES_PER_PAGE)
680 size = ENTRIES_PER_PAGE;
681 offset = shmem_find_swp(entry, ptr, ptr+size);
683 shmem_dir_unmap(dir);
686 shmem_swp_unmap(ptr);
690 shmem_dir_unmap(dir-1);
692 spin_unlock(&info->lock);
696 inode = &info->vfs_inode;
697 if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
698 info->flags |= SHMEM_PAGEIN;
699 shmem_swp_set(info, ptr + offset, 0);
701 shmem_swp_unmap(ptr);
702 spin_unlock(&info->lock);
704 * Decrement swap count even when the entry is left behind:
705 * try_to_unuse will skip over mms, then reincrement count.
712 * shmem_unuse() search for an eventually swapped out shmem page.
714 int shmem_unuse(swp_entry_t entry, struct page *page)
717 struct shmem_inode_info *info;
720 spin_lock(&shmem_ilock);
721 list_for_each(p, &shmem_inodes) {
722 info = list_entry(p, struct shmem_inode_info, list);
724 if (info->swapped && shmem_unuse_inode(info, entry, page)) {
725 /* move head to start search for next from here */
726 list_move_tail(&shmem_inodes, &info->list);
731 spin_unlock(&shmem_ilock);
736 * Move the page from the page cache to the swap cache.
738 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
740 struct shmem_inode_info *info;
741 swp_entry_t *entry, swap;
742 struct address_space *mapping;
746 BUG_ON(!PageLocked(page));
747 BUG_ON(page_mapped(page));
749 mapping = page->mapping;
751 inode = mapping->host;
752 info = SHMEM_I(inode);
753 if (info->flags & VM_LOCKED)
755 swap = get_swap_page();
759 spin_lock(&info->lock);
760 shmem_recalc_inode(inode);
761 if (index >= info->next_index) {
762 BUG_ON(!(info->flags & SHMEM_TRUNCATE));
765 entry = shmem_swp_entry(info, index, NULL);
769 if (move_to_swap_cache(page, swap) == 0) {
770 shmem_swp_set(info, entry, swap.val);
771 shmem_swp_unmap(entry);
772 spin_unlock(&info->lock);
777 shmem_swp_unmap(entry);
779 spin_unlock(&info->lock);
782 set_page_dirty(page);
783 return WRITEPAGE_ACTIVATE; /* Return with the page locked */
787 * shmem_getpage - either get the page from swap or allocate a new one
789 * If we allocate a new one we do not mark it dirty. That's up to the
790 * vm. If we swap it in we mark it dirty since we also free the swap
791 * entry since a page cannot live in both the swap and page cache
793 static int shmem_getpage(struct inode *inode, unsigned long idx, struct page **pagep, enum sgp_type sgp, int *type)
795 struct address_space *mapping = inode->i_mapping;
796 struct shmem_inode_info *info = SHMEM_I(inode);
797 struct shmem_sb_info *sbinfo;
798 struct page *filepage = *pagep;
799 struct page *swappage;
802 int error, majmin = VM_FAULT_MINOR;
804 if (idx >= SHMEM_MAX_INDEX)
807 * Normally, filepage is NULL on entry, and either found
808 * uptodate immediately, or allocated and zeroed, or read
809 * in under swappage, which is then assigned to filepage.
810 * But shmem_prepare_write passes in a locked filepage,
811 * which may be found not uptodate by other callers too,
812 * and may need to be copied from the swappage read in.
816 filepage = find_lock_page(mapping, idx);
817 if (filepage && PageUptodate(filepage))
820 if (sgp == SGP_QUICK)
823 spin_lock(&info->lock);
824 shmem_recalc_inode(inode);
825 entry = shmem_swp_alloc(info, idx, sgp);
827 spin_unlock(&info->lock);
828 error = PTR_ERR(entry);
834 /* Look it up and read it in.. */
835 swappage = lookup_swap_cache(swap);
837 shmem_swp_unmap(entry);
838 spin_unlock(&info->lock);
839 /* here we actually do the io */
840 if (majmin == VM_FAULT_MINOR && type)
841 inc_page_state(pgmajfault);
842 majmin = VM_FAULT_MAJOR;
843 swapin_readahead(swap);
844 swappage = read_swap_cache_async(swap);
846 spin_lock(&info->lock);
847 entry = shmem_swp_alloc(info, idx, sgp);
849 error = PTR_ERR(entry);
851 if (entry->val == swap.val)
853 shmem_swp_unmap(entry);
855 spin_unlock(&info->lock);
860 wait_on_page_locked(swappage);
861 page_cache_release(swappage);
865 /* We have to do this with page locked to prevent races */
866 if (TestSetPageLocked(swappage)) {
867 shmem_swp_unmap(entry);
868 spin_unlock(&info->lock);
869 wait_on_page_locked(swappage);
870 page_cache_release(swappage);
873 if (PageWriteback(swappage)) {
874 shmem_swp_unmap(entry);
875 spin_unlock(&info->lock);
876 wait_on_page_writeback(swappage);
877 unlock_page(swappage);
878 page_cache_release(swappage);
881 if (!PageUptodate(swappage)) {
882 shmem_swp_unmap(entry);
883 spin_unlock(&info->lock);
884 unlock_page(swappage);
885 page_cache_release(swappage);
891 shmem_swp_set(info, entry, 0);
892 shmem_swp_unmap(entry);
893 delete_from_swap_cache(swappage);
894 spin_unlock(&info->lock);
895 copy_highpage(filepage, swappage);
896 unlock_page(swappage);
897 page_cache_release(swappage);
898 flush_dcache_page(filepage);
899 SetPageUptodate(filepage);
900 set_page_dirty(filepage);
902 } else if (!(error = move_from_swap_cache(
903 swappage, idx, mapping))) {
904 info->flags |= SHMEM_PAGEIN;
905 shmem_swp_set(info, entry, 0);
906 shmem_swp_unmap(entry);
907 spin_unlock(&info->lock);
911 shmem_swp_unmap(entry);
912 spin_unlock(&info->lock);
913 unlock_page(swappage);
914 page_cache_release(swappage);
915 if (error == -ENOMEM) {
916 /* let kswapd refresh zone for GFP_ATOMICs */
917 blk_congestion_wait(WRITE, HZ/50);
921 } else if (sgp == SGP_READ && !filepage) {
922 shmem_swp_unmap(entry);
923 filepage = find_get_page(mapping, idx);
925 (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
926 spin_unlock(&info->lock);
927 wait_on_page_locked(filepage);
928 page_cache_release(filepage);
932 spin_unlock(&info->lock);
934 shmem_swp_unmap(entry);
935 sbinfo = SHMEM_SB(inode->i_sb);
936 spin_lock(&sbinfo->stat_lock);
937 if (sbinfo->free_blocks == 0 || shmem_acct_block(info->flags)) {
938 spin_unlock(&sbinfo->stat_lock);
939 spin_unlock(&info->lock);
943 sbinfo->free_blocks--;
944 inode->i_blocks += BLOCKS_PER_PAGE;
945 spin_unlock(&sbinfo->stat_lock);
948 spin_unlock(&info->lock);
949 filepage = page_cache_alloc(mapping);
951 shmem_unacct_blocks(info->flags, 1);
952 shmem_free_block(inode);
957 spin_lock(&info->lock);
958 entry = shmem_swp_alloc(info, idx, sgp);
960 error = PTR_ERR(entry);
963 shmem_swp_unmap(entry);
965 if (error || swap.val || 0 != add_to_page_cache_lru(
966 filepage, mapping, idx, GFP_ATOMIC)) {
967 spin_unlock(&info->lock);
968 page_cache_release(filepage);
969 shmem_unacct_blocks(info->flags, 1);
970 shmem_free_block(inode);
976 info->flags |= SHMEM_PAGEIN;
980 spin_unlock(&info->lock);
981 clear_highpage(filepage);
982 flush_dcache_page(filepage);
983 SetPageUptodate(filepage);
988 unlock_page(filepage);
991 *pagep = ZERO_PAGE(0);
998 if (*pagep != filepage) {
999 unlock_page(filepage);
1000 page_cache_release(filepage);
1005 struct page *shmem_nopage(struct vm_area_struct *vma, unsigned long address, int *type)
1007 struct inode *inode = vma->vm_file->f_dentry->d_inode;
1008 struct page *page = NULL;
1012 idx = (address - vma->vm_start) >> PAGE_SHIFT;
1013 idx += vma->vm_pgoff;
1014 idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
1016 error = shmem_getpage(inode, idx, &page, SGP_CACHE, type);
1018 return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS;
1020 mark_page_accessed(page);
1024 static int shmem_populate(struct vm_area_struct *vma,
1025 unsigned long addr, unsigned long len,
1026 pgprot_t prot, unsigned long pgoff, int nonblock)
1028 struct inode *inode = vma->vm_file->f_dentry->d_inode;
1029 struct mm_struct *mm = vma->vm_mm;
1030 enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE;
1033 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1034 if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size)
1037 while ((long) len > 0) {
1038 struct page *page = NULL;
1041 * Will need changing if PAGE_CACHE_SIZE != PAGE_SIZE
1043 err = shmem_getpage(inode, pgoff, &page, sgp, NULL);
1047 mark_page_accessed(page);
1048 err = install_page(mm, vma, addr, page, prot);
1050 page_cache_release(page);
1053 } else if (nonblock) {
1055 * If a nonlinear mapping then store the file page
1056 * offset in the pte.
1058 if (pgoff != linear_page_index(vma, addr)) {
1059 err = install_file_pte(mm, vma, addr, pgoff, prot);
1072 void shmem_lock(struct file *file, int lock)
1074 struct inode *inode = file->f_dentry->d_inode;
1075 struct shmem_inode_info *info = SHMEM_I(inode);
1077 spin_lock(&info->lock);
1079 info->flags |= VM_LOCKED;
1081 info->flags &= ~VM_LOCKED;
1082 spin_unlock(&info->lock);
1085 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1087 file_accessed(file);
1088 vma->vm_ops = &shmem_vm_ops;
1092 static struct inode *
1093 shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
1095 struct inode *inode;
1096 struct shmem_inode_info *info;
1097 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1099 spin_lock(&sbinfo->stat_lock);
1100 if (!sbinfo->free_inodes) {
1101 spin_unlock(&sbinfo->stat_lock);
1104 sbinfo->free_inodes--;
1105 spin_unlock(&sbinfo->stat_lock);
1107 inode = new_inode(sb);
1109 inode->i_mode = mode;
1110 inode->i_uid = current->fsuid;
1111 inode->i_gid = current->fsgid;
1112 inode->i_blksize = PAGE_CACHE_SIZE;
1113 inode->i_blocks = 0;
1114 inode->i_mapping->a_ops = &shmem_aops;
1115 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1116 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1117 info = SHMEM_I(inode);
1118 memset(info, 0, (char *)inode - (char *)info);
1119 spin_lock_init(&info->lock);
1120 switch (mode & S_IFMT) {
1122 init_special_inode(inode, mode, dev);
1125 inode->i_op = &shmem_inode_operations;
1126 inode->i_fop = &shmem_file_operations;
1127 spin_lock(&shmem_ilock);
1128 list_add_tail(&info->list, &shmem_inodes);
1129 spin_unlock(&shmem_ilock);
1133 /* Some things misbehave if size == 0 on a directory */
1134 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1135 inode->i_op = &shmem_dir_inode_operations;
1136 inode->i_fop = &simple_dir_operations;
1145 static int shmem_set_size(struct shmem_sb_info *info,
1146 unsigned long max_blocks, unsigned long max_inodes)
1149 unsigned long blocks, inodes;
1151 spin_lock(&info->stat_lock);
1152 blocks = info->max_blocks - info->free_blocks;
1153 inodes = info->max_inodes - info->free_inodes;
1155 if (max_blocks < blocks)
1157 if (max_inodes < inodes)
1160 info->max_blocks = max_blocks;
1161 info->free_blocks = max_blocks - blocks;
1162 info->max_inodes = max_inodes;
1163 info->free_inodes = max_inodes - inodes;
1165 spin_unlock(&info->stat_lock);
1171 static struct inode_operations shmem_symlink_inode_operations;
1172 static struct inode_operations shmem_symlink_inline_operations;
1175 * Normally tmpfs makes no use of shmem_prepare_write, but it
1176 * lets a tmpfs file be used read-write below the loop driver.
1179 shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1181 struct inode *inode = page->mapping->host;
1182 return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL);
1186 shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
1188 struct inode *inode = file->f_dentry->d_inode;
1190 unsigned long written;
1193 if ((ssize_t) count < 0)
1196 if (!access_ok(VERIFY_READ, buf, count))
1199 down(&inode->i_sem);
1204 err = generic_write_checks(file, &pos, &count, 0);
1208 err = remove_suid(file->f_dentry);
1212 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1215 struct page *page = NULL;
1216 unsigned long bytes, index, offset;
1220 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1221 index = pos >> PAGE_CACHE_SHIFT;
1222 bytes = PAGE_CACHE_SIZE - offset;
1227 * We don't hold page lock across copy from user -
1228 * what would it guard against? - so no deadlock here.
1229 * But it still may be a good idea to prefault below.
1232 err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
1237 if (PageHighMem(page)) {
1238 volatile unsigned char dummy;
1239 __get_user(dummy, buf);
1240 __get_user(dummy, buf + bytes - 1);
1242 kaddr = kmap_atomic(page, KM_USER0);
1243 left = __copy_from_user(kaddr + offset, buf, bytes);
1244 kunmap_atomic(kaddr, KM_USER0);
1248 left = __copy_from_user(kaddr + offset, buf, bytes);
1256 if (pos > inode->i_size)
1257 i_size_write(inode, pos);
1259 flush_dcache_page(page);
1260 set_page_dirty(page);
1261 mark_page_accessed(page);
1262 page_cache_release(page);
1272 * Our dirty pages are not counted in nr_dirty,
1273 * and we do not attempt to balance dirty pages.
1287 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1289 struct inode *inode = filp->f_dentry->d_inode;
1290 struct address_space *mapping = inode->i_mapping;
1291 unsigned long index, offset;
1293 index = *ppos >> PAGE_CACHE_SHIFT;
1294 offset = *ppos & ~PAGE_CACHE_MASK;
1297 struct page *page = NULL;
1298 unsigned long end_index, nr, ret;
1299 loff_t i_size = i_size_read(inode);
1301 end_index = i_size >> PAGE_CACHE_SHIFT;
1302 if (index > end_index)
1304 if (index == end_index) {
1305 nr = i_size & ~PAGE_CACHE_MASK;
1310 desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
1312 if (desc->error == -EINVAL)
1318 * We must evaluate after, since reads (unlike writes)
1319 * are called without i_sem protection against truncate
1321 nr = PAGE_CACHE_SIZE;
1322 i_size = i_size_read(inode);
1323 end_index = i_size >> PAGE_CACHE_SHIFT;
1324 if (index == end_index) {
1325 nr = i_size & ~PAGE_CACHE_MASK;
1327 page_cache_release(page);
1333 if (page != ZERO_PAGE(0)) {
1335 * If users can be writing to this page using arbitrary
1336 * virtual addresses, take care about potential aliasing
1337 * before reading the page on the kernel side.
1339 if (mapping_writably_mapped(mapping))
1340 flush_dcache_page(page);
1342 * Mark the page accessed if we read the beginning.
1345 mark_page_accessed(page);
1349 * Ok, we have the page, and it's up-to-date, so
1350 * now we can copy it to user space...
1352 * The actor routine returns how many bytes were actually used..
1353 * NOTE! This may not be the same as how much of a user buffer
1354 * we filled up (we may be padding etc), so we can only update
1355 * "pos" here (the actor routine has to update the user buffer
1356 * pointers and the remaining count).
1358 ret = actor(desc, page, offset, nr);
1360 index += offset >> PAGE_CACHE_SHIFT;
1361 offset &= ~PAGE_CACHE_MASK;
1363 page_cache_release(page);
1364 if (ret != nr || !desc->count)
1370 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1371 file_accessed(filp);
1374 static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1376 read_descriptor_t desc;
1378 if ((ssize_t) count < 0)
1380 if (!access_ok(VERIFY_WRITE, buf, count))
1390 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1392 return desc.written;
1396 static ssize_t shmem_file_sendfile(struct file *in_file, loff_t *ppos,
1397 size_t count, read_actor_t actor, void __user *target)
1399 read_descriptor_t desc;
1409 do_shmem_file_read(in_file, ppos, &desc, actor);
1411 return desc.written;
1415 static int shmem_statfs(struct super_block *sb, struct kstatfs *buf)
1417 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1419 buf->f_type = TMPFS_MAGIC;
1420 buf->f_bsize = PAGE_CACHE_SIZE;
1421 spin_lock(&sbinfo->stat_lock);
1422 buf->f_blocks = sbinfo->max_blocks;
1423 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
1424 buf->f_files = sbinfo->max_inodes;
1425 buf->f_ffree = sbinfo->free_inodes;
1426 spin_unlock(&sbinfo->stat_lock);
1427 buf->f_namelen = NAME_MAX;
1432 * File creation. Allocate an inode, and we're done..
1435 shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1437 struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
1438 int error = -ENOSPC;
1441 if (dir->i_mode & S_ISGID) {
1442 inode->i_gid = dir->i_gid;
1444 inode->i_mode |= S_ISGID;
1446 dir->i_size += BOGO_DIRENT_SIZE;
1447 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1448 d_instantiate(dentry, inode);
1449 dget(dentry); /* Extra count - pin the dentry in core */
1455 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1459 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1465 static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1466 struct nameidata *nd)
1468 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1474 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1476 struct inode *inode = old_dentry->d_inode;
1478 dir->i_size += BOGO_DIRENT_SIZE;
1479 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1481 atomic_inc(&inode->i_count); /* New dentry reference */
1482 dget(dentry); /* Extra pinning count for the created dentry */
1483 d_instantiate(dentry, inode);
1487 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1489 struct inode *inode = dentry->d_inode;
1491 dir->i_size -= BOGO_DIRENT_SIZE;
1492 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1494 dput(dentry); /* Undo the count from "create" - this does all the work */
1498 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1500 if (!simple_empty(dentry))
1504 return shmem_unlink(dir, dentry);
1508 * The VFS layer already does all the dentry stuff for rename,
1509 * we just have to decrement the usage count for the target if
1510 * it exists so that the VFS layer correctly free's it when it
1513 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1515 struct inode *inode = old_dentry->d_inode;
1516 int they_are_dirs = S_ISDIR(inode->i_mode);
1518 if (!simple_empty(new_dentry))
1521 if (new_dentry->d_inode) {
1522 (void) shmem_unlink(new_dir, new_dentry);
1525 } else if (they_are_dirs) {
1530 old_dir->i_size -= BOGO_DIRENT_SIZE;
1531 new_dir->i_size += BOGO_DIRENT_SIZE;
1532 old_dir->i_ctime = old_dir->i_mtime =
1533 new_dir->i_ctime = new_dir->i_mtime =
1534 inode->i_ctime = CURRENT_TIME;
1538 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1542 struct inode *inode;
1543 struct page *page = NULL;
1545 struct shmem_inode_info *info;
1547 len = strlen(symname) + 1;
1548 if (len > PAGE_CACHE_SIZE)
1549 return -ENAMETOOLONG;
1551 inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
1555 info = SHMEM_I(inode);
1556 inode->i_size = len-1;
1557 if (len <= (char *)inode - (char *)info) {
1559 memcpy(info, symname, len);
1560 inode->i_op = &shmem_symlink_inline_operations;
1562 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1567 inode->i_op = &shmem_symlink_inode_operations;
1568 spin_lock(&shmem_ilock);
1569 list_add_tail(&info->list, &shmem_inodes);
1570 spin_unlock(&shmem_ilock);
1571 kaddr = kmap_atomic(page, KM_USER0);
1572 memcpy(kaddr, symname, len);
1573 kunmap_atomic(kaddr, KM_USER0);
1574 set_page_dirty(page);
1575 page_cache_release(page);
1577 if (dir->i_mode & S_ISGID)
1578 inode->i_gid = dir->i_gid;
1579 dir->i_size += BOGO_DIRENT_SIZE;
1580 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1581 d_instantiate(dentry, inode);
1586 static int shmem_readlink_inline(struct dentry *dentry, char __user *buffer, int buflen)
1588 return vfs_readlink(dentry, buffer, buflen, (const char *)SHMEM_I(dentry->d_inode));
1591 static int shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1593 return vfs_follow_link(nd, (const char *)SHMEM_I(dentry->d_inode));
1596 static int shmem_readlink(struct dentry *dentry, char __user *buffer, int buflen)
1598 struct page *page = NULL;
1599 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1602 res = vfs_readlink(dentry, buffer, buflen, kmap(page));
1604 mark_page_accessed(page);
1605 page_cache_release(page);
1609 static int shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1611 struct page *page = NULL;
1612 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1615 res = vfs_follow_link(nd, kmap(page));
1617 mark_page_accessed(page);
1618 page_cache_release(page);
1622 static struct inode_operations shmem_symlink_inline_operations = {
1623 .readlink = shmem_readlink_inline,
1624 .follow_link = shmem_follow_link_inline,
1627 static struct inode_operations shmem_symlink_inode_operations = {
1628 .truncate = shmem_truncate,
1629 .readlink = shmem_readlink,
1630 .follow_link = shmem_follow_link,
1633 static int shmem_parse_options(char *options, int *mode, uid_t *uid, gid_t *gid, unsigned long *blocks, unsigned long *inodes)
1635 char *this_char, *value, *rest;
1637 while ((this_char = strsep(&options, ",")) != NULL) {
1640 if ((value = strchr(this_char,'=')) != NULL) {
1644 "tmpfs: No value for mount option '%s'\n",
1649 if (!strcmp(this_char,"size")) {
1650 unsigned long long size;
1651 size = memparse(value,&rest);
1653 size <<= PAGE_SHIFT;
1654 size *= totalram_pages;
1660 *blocks = size >> PAGE_CACHE_SHIFT;
1661 } else if (!strcmp(this_char,"nr_blocks")) {
1662 *blocks = memparse(value,&rest);
1665 } else if (!strcmp(this_char,"nr_inodes")) {
1666 *inodes = memparse(value,&rest);
1669 } else if (!strcmp(this_char,"mode")) {
1672 *mode = simple_strtoul(value,&rest,8);
1675 } else if (!strcmp(this_char,"uid")) {
1678 *uid = simple_strtoul(value,&rest,0);
1681 } else if (!strcmp(this_char,"gid")) {
1684 *gid = simple_strtoul(value,&rest,0);
1688 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
1696 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
1702 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
1704 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1705 unsigned long max_blocks = sbinfo->max_blocks;
1706 unsigned long max_inodes = sbinfo->max_inodes;
1708 if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks, &max_inodes))
1710 return shmem_set_size(sbinfo, max_blocks, max_inodes);
1714 static int shmem_fill_super(struct super_block *sb,
1715 void *data, int silent)
1717 struct inode *inode;
1718 struct dentry *root;
1719 unsigned long blocks, inodes;
1720 int mode = S_IRWXUGO | S_ISVTX;
1721 uid_t uid = current->fsuid;
1722 gid_t gid = current->fsgid;
1723 struct shmem_sb_info *sbinfo;
1726 sbinfo = kmalloc(sizeof(struct shmem_sb_info), GFP_KERNEL);
1729 sb->s_fs_info = sbinfo;
1730 memset(sbinfo, 0, sizeof(struct shmem_sb_info));
1733 * Per default we only allow half of the physical ram per
1736 blocks = inodes = totalram_pages / 2;
1739 if (shmem_parse_options(data, &mode, &uid, &gid, &blocks, &inodes)) {
1744 sb->s_flags |= MS_NOUSER;
1747 spin_lock_init(&sbinfo->stat_lock);
1748 sbinfo->max_blocks = blocks;
1749 sbinfo->free_blocks = blocks;
1750 sbinfo->max_inodes = inodes;
1751 sbinfo->free_inodes = inodes;
1752 sb->s_maxbytes = SHMEM_MAX_BYTES;
1753 sb->s_blocksize = PAGE_CACHE_SIZE;
1754 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1755 sb->s_magic = TMPFS_MAGIC;
1756 sb->s_op = &shmem_ops;
1757 inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
1762 root = d_alloc_root(inode);
1772 sb->s_fs_info = NULL;
1776 static void shmem_put_super(struct super_block *sb)
1778 kfree(sb->s_fs_info);
1779 sb->s_fs_info = NULL;
1782 static kmem_cache_t *shmem_inode_cachep;
1784 static struct inode *shmem_alloc_inode(struct super_block *sb)
1786 struct shmem_inode_info *p;
1787 p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, SLAB_KERNEL);
1790 return &p->vfs_inode;
1793 static void shmem_destroy_inode(struct inode *inode)
1795 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
1798 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
1800 struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
1802 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1803 SLAB_CTOR_CONSTRUCTOR) {
1804 inode_init_once(&p->vfs_inode);
1808 static int init_inodecache(void)
1810 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
1811 sizeof(struct shmem_inode_info),
1812 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1814 if (shmem_inode_cachep == NULL)
1819 static void destroy_inodecache(void)
1821 if (kmem_cache_destroy(shmem_inode_cachep))
1822 printk(KERN_INFO "shmem_inode_cache: not all structures were freed\n");
1825 static struct address_space_operations shmem_aops = {
1826 .writepage = shmem_writepage,
1827 .set_page_dirty = __set_page_dirty_nobuffers,
1829 .prepare_write = shmem_prepare_write,
1830 .commit_write = simple_commit_write,
1834 static struct file_operations shmem_file_operations = {
1837 .llseek = generic_file_llseek,
1838 .read = shmem_file_read,
1839 .write = shmem_file_write,
1840 .fsync = simple_sync_file,
1841 .sendfile = shmem_file_sendfile,
1845 static struct inode_operations shmem_inode_operations = {
1846 .truncate = shmem_truncate,
1847 .setattr = shmem_notify_change,
1850 static struct inode_operations shmem_dir_inode_operations = {
1852 .create = shmem_create,
1853 .lookup = simple_lookup,
1855 .unlink = shmem_unlink,
1856 .symlink = shmem_symlink,
1857 .mkdir = shmem_mkdir,
1858 .rmdir = shmem_rmdir,
1859 .mknod = shmem_mknod,
1860 .rename = shmem_rename,
1864 static struct super_operations shmem_ops = {
1865 .alloc_inode = shmem_alloc_inode,
1866 .destroy_inode = shmem_destroy_inode,
1868 .statfs = shmem_statfs,
1869 .remount_fs = shmem_remount_fs,
1871 .delete_inode = shmem_delete_inode,
1872 .drop_inode = generic_delete_inode,
1873 .put_super = shmem_put_super,
1876 static struct vm_operations_struct shmem_vm_ops = {
1877 .nopage = shmem_nopage,
1878 .populate = shmem_populate,
1881 static struct super_block *shmem_get_sb(struct file_system_type *fs_type,
1882 int flags, const char *dev_name, void *data)
1884 return get_sb_nodev(fs_type, flags, data, shmem_fill_super);
1887 static struct file_system_type tmpfs_fs_type = {
1888 .owner = THIS_MODULE,
1890 .get_sb = shmem_get_sb,
1891 .kill_sb = kill_litter_super,
1893 static struct vfsmount *shm_mnt;
1895 static int __init init_tmpfs(void)
1899 error = init_inodecache();
1903 error = register_filesystem(&tmpfs_fs_type);
1905 printk(KERN_ERR "Could not register tmpfs\n");
1909 devfs_mk_dir("shm");
1911 shm_mnt = kern_mount(&tmpfs_fs_type);
1912 if (IS_ERR(shm_mnt)) {
1913 error = PTR_ERR(shm_mnt);
1914 printk(KERN_ERR "Could not kern_mount tmpfs\n");
1918 /* The internal instance should not do size checking */
1919 shmem_set_size(SHMEM_SB(shm_mnt->mnt_sb), ULONG_MAX, ULONG_MAX);
1923 unregister_filesystem(&tmpfs_fs_type);
1925 destroy_inodecache();
1927 shm_mnt = ERR_PTR(error);
1930 module_init(init_tmpfs)
1933 * shmem_file_setup - get an unlinked file living in tmpfs
1935 * @name: name for dentry (to be seen in /proc/<pid>/maps
1936 * @size: size to be set for the file
1939 struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
1943 struct inode *inode;
1944 struct dentry *dentry, *root;
1947 if (IS_ERR(shm_mnt))
1948 return (void *)shm_mnt;
1950 if (size > SHMEM_MAX_BYTES)
1951 return ERR_PTR(-EINVAL);
1953 if (shmem_acct_size(flags, size))
1954 return ERR_PTR(-ENOMEM);
1958 this.len = strlen(name);
1959 this.hash = 0; /* will go */
1960 root = shm_mnt->mnt_root;
1961 dentry = d_alloc(root, &this);
1966 file = get_empty_filp();
1971 inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
1975 SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
1976 d_instantiate(dentry, inode);
1977 inode->i_size = size;
1978 inode->i_nlink = 0; /* It is unlinked */
1979 file->f_vfsmnt = mntget(shm_mnt);
1980 file->f_dentry = dentry;
1981 file->f_mapping = inode->i_mapping;
1982 file->f_op = &shmem_file_operations;
1983 file->f_mode = FMODE_WRITE | FMODE_READ;
1991 shmem_unacct_size(flags, size);
1992 return ERR_PTR(error);
1996 * shmem_zero_setup - setup a shared anonymous mapping
1998 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2000 int shmem_zero_setup(struct vm_area_struct *vma)
2003 loff_t size = vma->vm_end - vma->vm_start;
2005 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2007 return PTR_ERR(file);
2011 vma->vm_file = file;
2012 vma->vm_ops = &shmem_vm_ops;
2016 EXPORT_SYMBOL(shmem_file_setup);