tiny-shmem.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include <linux/fs.h>
  13. #include <linux/init.h>
  14. #include <linux/devfs_fs_kernel.h>
  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. static struct file_system_type tmpfs_fs_type = {
  23. .name = "tmpfs",
  24. .get_sb = ramfs_get_sb,
  25. .kill_sb = kill_litter_super,
  26. };
  27. static struct vfsmount *shm_mnt;
  28. static int __init init_tmpfs(void)
  29. {
  30. BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
  31. #ifdef CONFIG_TMPFS
  32. devfs_mk_dir("shm");
  33. #endif
  34. shm_mnt = kern_mount(&tmpfs_fs_type);
  35. BUG_ON(IS_ERR(shm_mnt));
  36. return 0;
  37. }
  38. module_init(init_tmpfs)
  39. /*
  40. * shmem_file_setup - get an unlinked file living in tmpfs
  41. *
  42. * @name: name for dentry (to be seen in /proc/<pid>/maps
  43. * @size: size to be set for the file
  44. *
  45. */
  46. struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
  47. {
  48. int error;
  49. struct file *file;
  50. struct inode *inode;
  51. struct dentry *dentry, *root;
  52. struct qstr this;
  53. if (IS_ERR(shm_mnt))
  54. return (void *)shm_mnt;
  55. error = -ENOMEM;
  56. this.name = name;
  57. this.len = strlen(name);
  58. this.hash = 0; /* will go */
  59. root = shm_mnt->mnt_root;
  60. dentry = d_alloc(root, &this);
  61. if (!dentry)
  62. goto put_memory;
  63. error = -ENFILE;
  64. file = get_empty_filp();
  65. if (!file)
  66. goto put_dentry;
  67. error = -ENOSPC;
  68. inode = ramfs_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
  69. if (!inode)
  70. goto close_file;
  71. d_instantiate(dentry, inode);
  72. inode->i_size = size;
  73. inode->i_nlink = 0; /* It is unlinked */
  74. file->f_vfsmnt = mntget(shm_mnt);
  75. file->f_dentry = dentry;
  76. file->f_mapping = inode->i_mapping;
  77. file->f_op = &ramfs_file_operations;
  78. file->f_mode = FMODE_WRITE | FMODE_READ;
  79. return file;
  80. close_file:
  81. put_filp(file);
  82. put_dentry:
  83. dput(dentry);
  84. put_memory:
  85. return ERR_PTR(error);
  86. }
  87. /*
  88. * shmem_zero_setup - setup a shared anonymous mapping
  89. *
  90. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  91. */
  92. int shmem_zero_setup(struct vm_area_struct *vma)
  93. {
  94. struct file *file;
  95. loff_t size = vma->vm_end - vma->vm_start;
  96. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  97. if (IS_ERR(file))
  98. return PTR_ERR(file);
  99. if (vma->vm_file)
  100. fput(vma->vm_file);
  101. vma->vm_file = file;
  102. vma->vm_ops = &generic_file_vm_ops;
  103. return 0;
  104. }
  105. int shmem_unuse(swp_entry_t entry, struct page *page)
  106. {
  107. return 0;
  108. }