blob: f0f55875dd6acc34d3c9ee05298cd43dcea02650 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tiny-shmem.c: simple shmemfs and tmpfs using ramfs code
3 *
4 * Matt Mackall <mpm@selenic.com> January, 2004
5 * derived from mm/shmem.c and fs/ramfs/inode.c
6 *
7 * This is intended for small system where the benefits of the full
8 * shmem code (swap-backed and resource-limited) are outweighed by
9 * their complexity. On systems without swap this code should be
10 * effectively equivalent, but much lighter weight.
11 */
12
13#include <linux/fs.h>
14#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/vfs.h>
16#include <linux/mount.h>
17#include <linux/file.h>
18#include <linux/mm.h>
19#include <linux/module.h>
20#include <linux/swap.h>
21#include <linux/ramfs.h>
22
23static struct file_system_type tmpfs_fs_type = {
24 .name = "tmpfs",
25 .get_sb = ramfs_get_sb,
26 .kill_sb = kill_litter_super,
27};
28
29static struct vfsmount *shm_mnt;
30
31static int __init init_tmpfs(void)
32{
Matt Mackall5d57bd32005-10-30 15:03:18 -080033 BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 shm_mnt = kern_mount(&tmpfs_fs_type);
Matt Mackall5d57bd32005-10-30 15:03:18 -080036 BUG_ON(IS_ERR(shm_mnt));
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return 0;
39}
40module_init(init_tmpfs)
41
Randy Dunlap46711812008-03-19 17:00:41 -070042/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * @name: name for dentry (to be seen in /proc/<pid>/maps
45 * @size: size to be set for the file
Randy Dunlap46711812008-03-19 17:00:41 -070046 * @flags: vm_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
48struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
49{
50 int error;
51 struct file *file;
52 struct inode *inode;
53 struct dentry *dentry, *root;
54 struct qstr this;
55
56 if (IS_ERR(shm_mnt))
57 return (void *)shm_mnt;
58
59 error = -ENOMEM;
60 this.name = name;
61 this.len = strlen(name);
62 this.hash = 0; /* will go */
63 root = shm_mnt->mnt_root;
64 dentry = d_alloc(root, &this);
65 if (!dentry)
66 goto put_memory;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 error = -ENOSPC;
69 inode = ramfs_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
70 if (!inode)
Dave Hansence8d2cd2007-10-16 23:31:13 -070071 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 d_instantiate(dentry, inode);
Dave Hansence8d2cd2007-10-16 23:31:13 -070074 error = -ENFILE;
75 file = alloc_file(shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
76 &ramfs_file_operations);
77 if (!file)
78 goto put_dentry;
David Howellsb0e15192006-01-06 00:11:42 -080079
Dave Hansence8d2cd2007-10-16 23:31:13 -070080 inode->i_nlink = 0; /* It is unlinked */
David Howellsb0e15192006-01-06 00:11:42 -080081
82 /* notify everyone as to the change of file size */
Catalin Marinas2a7e2f72006-01-12 01:05:21 -080083 error = do_truncate(dentry, size, 0, file);
David Howellsb0e15192006-01-06 00:11:42 -080084 if (error < 0)
85 goto close_file;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return file;
88
89close_file:
90 put_filp(file);
91put_dentry:
92 dput(dentry);
93put_memory:
94 return ERR_PTR(error);
95}
96
Randy Dunlap46711812008-03-19 17:00:41 -070097/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
100 */
101int shmem_zero_setup(struct vm_area_struct *vma)
102{
103 struct file *file;
104 loff_t size = vma->vm_end - vma->vm_start;
105
106 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
107 if (IS_ERR(file))
108 return PTR_ERR(file);
109
110 if (vma->vm_file)
111 fput(vma->vm_file);
112 vma->vm_file = file;
113 vma->vm_ops = &generic_file_vm_ops;
114 return 0;
115}
116
117int shmem_unuse(swp_entry_t entry, struct page *page)
118{
119 return 0;
120}
David Howellsb0e15192006-01-06 00:11:42 -0800121
David Howellsb0e15192006-01-06 00:11:42 -0800122#ifndef CONFIG_MMU
123unsigned long shmem_get_unmapped_area(struct file *file,
124 unsigned long addr,
125 unsigned long len,
126 unsigned long pgoff,
127 unsigned long flags)
128{
129 return ramfs_nommu_get_unmapped_area(file, addr, len, pgoff, flags);
130}
131#endif