tiny-shmem.c 3.0 KB

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