Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #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 | |
| 23 | static struct file_system_type tmpfs_fs_type = { |
| 24 | .name = "tmpfs", |
| 25 | .get_sb = ramfs_get_sb, |
| 26 | .kill_sb = kill_litter_super, |
| 27 | }; |
| 28 | |
| 29 | static struct vfsmount *shm_mnt; |
| 30 | |
| 31 | static int __init init_tmpfs(void) |
| 32 | { |
Matt Mackall | 5d57bd3 | 2005-10-30 15:03:18 -0800 | [diff] [blame] | 33 | BUG_ON(register_filesystem(&tmpfs_fs_type) != 0); |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | shm_mnt = kern_mount(&tmpfs_fs_type); |
Matt Mackall | 5d57bd3 | 2005-10-30 15:03:18 -0800 | [diff] [blame] | 36 | BUG_ON(IS_ERR(shm_mnt)); |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | module_init(init_tmpfs) |
| 41 | |
| 42 | /* |
| 43 | * shmem_file_setup - get an unlinked file living in tmpfs |
| 44 | * |
| 45 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
| 46 | * @size: size to be set for the file |
| 47 | * |
| 48 | */ |
| 49 | struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags) |
| 50 | { |
| 51 | int error; |
| 52 | struct file *file; |
| 53 | struct inode *inode; |
| 54 | struct dentry *dentry, *root; |
| 55 | struct qstr this; |
| 56 | |
| 57 | if (IS_ERR(shm_mnt)) |
| 58 | return (void *)shm_mnt; |
| 59 | |
| 60 | error = -ENOMEM; |
| 61 | this.name = name; |
| 62 | this.len = strlen(name); |
| 63 | this.hash = 0; /* will go */ |
| 64 | root = shm_mnt->mnt_root; |
| 65 | dentry = d_alloc(root, &this); |
| 66 | if (!dentry) |
| 67 | goto put_memory; |
| 68 | |
| 69 | error = -ENFILE; |
| 70 | file = get_empty_filp(); |
| 71 | if (!file) |
| 72 | goto put_dentry; |
| 73 | |
| 74 | error = -ENOSPC; |
| 75 | inode = ramfs_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0); |
| 76 | if (!inode) |
| 77 | goto close_file; |
| 78 | |
| 79 | d_instantiate(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | inode->i_nlink = 0; /* It is unlinked */ |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 81 | |
Josef Sipek | e9536ae | 2006-12-08 02:37:21 -0800 | [diff] [blame] | 82 | file->f_path.mnt = mntget(shm_mnt); |
| 83 | file->f_path.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | file->f_mapping = inode->i_mapping; |
| 85 | file->f_op = &ramfs_file_operations; |
| 86 | file->f_mode = FMODE_WRITE | FMODE_READ; |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 87 | |
| 88 | /* notify everyone as to the change of file size */ |
Catalin Marinas | 2a7e2f7 | 2006-01-12 01:05:21 -0800 | [diff] [blame] | 89 | error = do_truncate(dentry, size, 0, file); |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 90 | if (error < 0) |
| 91 | goto close_file; |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return file; |
| 94 | |
| 95 | close_file: |
| 96 | put_filp(file); |
| 97 | put_dentry: |
| 98 | dput(dentry); |
| 99 | put_memory: |
| 100 | return ERR_PTR(error); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * shmem_zero_setup - setup a shared anonymous mapping |
| 105 | * |
| 106 | * @vma: the vma to be mmapped is prepared by do_mmap_pgoff |
| 107 | */ |
| 108 | int shmem_zero_setup(struct vm_area_struct *vma) |
| 109 | { |
| 110 | struct file *file; |
| 111 | loff_t size = vma->vm_end - vma->vm_start; |
| 112 | |
| 113 | file = shmem_file_setup("dev/zero", size, vma->vm_flags); |
| 114 | if (IS_ERR(file)) |
| 115 | return PTR_ERR(file); |
| 116 | |
| 117 | if (vma->vm_file) |
| 118 | fput(vma->vm_file); |
| 119 | vma->vm_file = file; |
| 120 | vma->vm_ops = &generic_file_vm_ops; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int shmem_unuse(swp_entry_t entry, struct page *page) |
| 125 | { |
| 126 | return 0; |
| 127 | } |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 128 | |
| 129 | int shmem_mmap(struct file *file, struct vm_area_struct *vma) |
| 130 | { |
| 131 | file_accessed(file); |
| 132 | #ifndef CONFIG_MMU |
| 133 | return ramfs_nommu_mmap(file, vma); |
| 134 | #else |
| 135 | return 0; |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | #ifndef CONFIG_MMU |
| 140 | unsigned long shmem_get_unmapped_area(struct file *file, |
| 141 | unsigned long addr, |
| 142 | unsigned long len, |
| 143 | unsigned long pgoff, |
| 144 | unsigned long flags) |
| 145 | { |
| 146 | return ramfs_nommu_get_unmapped_area(file, addr, len, pgoff, flags); |
| 147 | } |
| 148 | #endif |