super.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. return !TestSetPageDirty(page);
  24. }
  25. static const struct address_space_operations xenfs_aops = {
  26. .set_page_dirty = xenfs_set_page_dirty,
  27. };
  28. static struct backing_dev_info xenfs_backing_dev_info = {
  29. .ra_pages = 0, /* No readahead */
  30. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  31. };
  32. static struct inode *xenfs_make_inode(struct super_block *sb, int mode)
  33. {
  34. struct inode *ret = new_inode(sb);
  35. if (ret) {
  36. ret->i_mode = mode;
  37. ret->i_mapping->a_ops = &xenfs_aops;
  38. ret->i_mapping->backing_dev_info = &xenfs_backing_dev_info;
  39. ret->i_uid = ret->i_gid = 0;
  40. ret->i_blocks = 0;
  41. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  42. }
  43. return ret;
  44. }
  45. static struct dentry *xenfs_create_file(struct super_block *sb,
  46. struct dentry *parent,
  47. const char *name,
  48. const struct file_operations *fops,
  49. void *data,
  50. int mode)
  51. {
  52. struct dentry *dentry;
  53. struct inode *inode;
  54. dentry = d_alloc_name(parent, name);
  55. if (!dentry)
  56. return NULL;
  57. inode = xenfs_make_inode(sb, S_IFREG | mode);
  58. if (!inode) {
  59. dput(dentry);
  60. return NULL;
  61. }
  62. inode->i_fop = fops;
  63. inode->i_private = data;
  64. d_add(dentry, inode);
  65. return dentry;
  66. }
  67. static ssize_t capabilities_read(struct file *file, char __user *buf,
  68. size_t size, loff_t *off)
  69. {
  70. char *tmp = "";
  71. if (xen_initial_domain())
  72. tmp = "control_d\n";
  73. return simple_read_from_buffer(buf, size, off, tmp, strlen(tmp));
  74. }
  75. static const struct file_operations capabilities_file_ops = {
  76. .read = capabilities_read,
  77. .llseek = default_llseek,
  78. };
  79. static int xenfs_fill_super(struct super_block *sb, void *data, int silent)
  80. {
  81. static struct tree_descr xenfs_files[] = {
  82. [1] = {},
  83. { "xenbus", &xenbus_file_ops, S_IRUSR|S_IWUSR },
  84. { "capabilities", &capabilities_file_ops, S_IRUGO },
  85. { "privcmd", &privcmd_file_ops, S_IRUSR|S_IWUSR },
  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. }
  98. return rc;
  99. }
  100. static int xenfs_mount(struct file_system_type *fs_type,
  101. int flags, const char *dev_name,
  102. void *data)
  103. {
  104. return mount_single(fs_type, flags, data, xenfs_fill_super);
  105. }
  106. static struct file_system_type xenfs_type = {
  107. .owner = THIS_MODULE,
  108. .name = "xenfs",
  109. .mount = xenfs_mount,
  110. .kill_sb = kill_litter_super,
  111. };
  112. static int __init xenfs_init(void)
  113. {
  114. int err;
  115. if (!xen_domain()) {
  116. printk(KERN_INFO "xenfs: not registering filesystem on non-xen platform\n");
  117. return 0;
  118. }
  119. err = register_filesystem(&xenfs_type);
  120. if (err) {
  121. printk(KERN_ERR "xenfs: Unable to register filesystem!\n");
  122. goto out;
  123. }
  124. err = bdi_init(&xenfs_backing_dev_info);
  125. if (err)
  126. unregister_filesystem(&xenfs_type);
  127. out:
  128. return err;
  129. }
  130. static void __exit xenfs_exit(void)
  131. {
  132. if (xen_domain())
  133. unregister_filesystem(&xenfs_type);
  134. }
  135. module_init(xenfs_init);
  136. module_exit(xenfs_exit);