super.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * xenfs.c - a filesystem for passing info between the a domain and
  3. * the hypervisor.
  4. *
  5. * 2008-10-07 Alex Zeffertt Replaced /proc/xen/xenbus with xenfs filesystem
  6. * and /proc/xen compatibility mount point.
  7. * Turned xenfs into a loadable module.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/magic.h>
  14. #include <linux/mm.h>
  15. #include <linux/backing-dev.h>
  16. #include <xen/xen.h>
  17. #include "xenfs.h"
  18. #include <asm/xen/hypervisor.h>
  19. MODULE_DESCRIPTION("Xen filesystem");
  20. MODULE_LICENSE("GPL");
  21. static int xenfs_set_page_dirty(struct page *page)
  22. {
  23. if (!PageDirty(page))
  24. SetPageDirty(page);
  25. return 0;
  26. }
  27. static const struct address_space_operations xenfs_aops = {
  28. .set_page_dirty = xenfs_set_page_dirty,
  29. };
  30. static struct backing_dev_info xenfs_backing_dev_info = {
  31. .ra_pages = 0, /* No readahead */
  32. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  33. };
  34. static struct inode *xenfs_make_inode(struct super_block *sb, int mode)
  35. {
  36. struct inode *ret = new_inode(sb);
  37. if (ret) {
  38. ret->i_mode = mode;
  39. ret->i_mapping->a_ops = &xenfs_aops;
  40. ret->i_mapping->backing_dev_info = &xenfs_backing_dev_info;
  41. ret->i_uid = ret->i_gid = 0;
  42. ret->i_blocks = 0;
  43. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  44. }
  45. return ret;
  46. }
  47. static struct dentry *xenfs_create_file(struct super_block *sb,
  48. struct dentry *parent,
  49. const char *name,
  50. const struct file_operations *fops,
  51. void *data,
  52. int mode)
  53. {
  54. struct dentry *dentry;
  55. struct inode *inode;
  56. dentry = d_alloc_name(parent, name);
  57. if (!dentry)
  58. return NULL;
  59. inode = xenfs_make_inode(sb, S_IFREG | mode);
  60. if (!inode) {
  61. dput(dentry);
  62. return NULL;
  63. }
  64. inode->i_fop = fops;
  65. inode->i_private = data;
  66. d_add(dentry, inode);
  67. return dentry;
  68. }
  69. static ssize_t capabilities_read(struct file *file, char __user *buf,
  70. size_t size, loff_t *off)
  71. {
  72. char *tmp = "";
  73. if (xen_initial_domain())
  74. tmp = "control_d\n";
  75. return simple_read_from_buffer(buf, size, off, tmp, strlen(tmp));
  76. }
  77. static const struct file_operations capabilities_file_ops = {
  78. .read = capabilities_read,
  79. };
  80. static int xenfs_fill_super(struct super_block *sb, void *data, int silent)
  81. {
  82. static struct tree_descr xenfs_files[] = {
  83. [1] = {},
  84. { "xenbus", &xenbus_file_ops, S_IRUSR|S_IWUSR },
  85. { "capabilities", &capabilities_file_ops, S_IRUGO },
  86. {""},
  87. };
  88. int rc;
  89. rc = simple_fill_super(sb, XENFS_SUPER_MAGIC, xenfs_files);
  90. if (rc < 0)
  91. return rc;
  92. if (xen_initial_domain()) {
  93. xenfs_create_file(sb, sb->s_root, "xsd_kva",
  94. &xsd_kva_file_ops, NULL, S_IRUSR|S_IWUSR);
  95. xenfs_create_file(sb, sb->s_root, "xsd_port",
  96. &xsd_port_file_ops, NULL, S_IRUSR|S_IWUSR);
  97. xenfs_create_file(sb, sb->s_root, "privcmd",
  98. &privcmd_file_ops, NULL, S_IRUSR|S_IWUSR);
  99. }
  100. return rc;
  101. }
  102. static int xenfs_get_sb(struct file_system_type *fs_type,
  103. int flags, const char *dev_name,
  104. void *data, struct vfsmount *mnt)
  105. {
  106. return get_sb_single(fs_type, flags, data, xenfs_fill_super, mnt);
  107. }
  108. static struct file_system_type xenfs_type = {
  109. .owner = THIS_MODULE,
  110. .name = "xenfs",
  111. .get_sb = xenfs_get_sb,
  112. .kill_sb = kill_litter_super,
  113. };
  114. static int __init xenfs_init(void)
  115. {
  116. int err;
  117. if (!xen_domain()) {
  118. printk(KERN_INFO "xenfs: not registering filesystem on non-xen platform\n");
  119. return 0;
  120. }
  121. err = register_filesystem(&xenfs_type);
  122. if (err) {
  123. printk(KERN_ERR "xenfs: Unable to register filesystem!\n");
  124. goto out;
  125. }
  126. err = bdi_init(&xenfs_backing_dev_info);
  127. if (err)
  128. unregister_filesystem(&xenfs_type);
  129. out:
  130. return err;
  131. }
  132. static void __exit xenfs_exit(void)
  133. {
  134. if (xen_domain())
  135. unregister_filesystem(&xenfs_type);
  136. }
  137. module_init(xenfs_init);
  138. module_exit(xenfs_exit);