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