kernfs.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * kernfs.h - pseudo filesystem decoupled from vfs locking
  3. *
  4. * This file is released under the GPLv2.
  5. */
  6. #ifndef __LINUX_KERNFS_H
  7. #define __LINUX_KERNFS_H
  8. #include <linux/kernel.h>
  9. #include <linux/err.h>
  10. #include <linux/list.h>
  11. #include <linux/mutex.h>
  12. #include <linux/lockdep.h>
  13. struct file;
  14. struct iattr;
  15. struct seq_file;
  16. struct vm_area_struct;
  17. struct sysfs_dirent;
  18. struct sysfs_open_file {
  19. /* published fields */
  20. struct sysfs_dirent *sd;
  21. struct file *file;
  22. /* private fields, do not use outside kernfs proper */
  23. struct mutex mutex;
  24. int event;
  25. struct list_head list;
  26. bool mmapped;
  27. const struct vm_operations_struct *vm_ops;
  28. };
  29. struct kernfs_ops {
  30. /*
  31. * Read is handled by either seq_file or raw_read().
  32. *
  33. * If seq_show() is present, seq_file path is active. Other seq
  34. * operations are optional and if not implemented, the behavior is
  35. * equivalent to single_open(). @sf->private points to the
  36. * associated sysfs_open_file.
  37. *
  38. * read() is bounced through kernel buffer and a read larger than
  39. * PAGE_SIZE results in partial operation of PAGE_SIZE.
  40. */
  41. int (*seq_show)(struct seq_file *sf, void *v);
  42. void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
  43. void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
  44. void (*seq_stop)(struct seq_file *sf, void *v);
  45. ssize_t (*read)(struct sysfs_open_file *of, char *buf, size_t bytes,
  46. loff_t off);
  47. /*
  48. * write() is bounced through kernel buffer and a write larger than
  49. * PAGE_SIZE results in partial operation of PAGE_SIZE.
  50. */
  51. ssize_t (*write)(struct sysfs_open_file *of, char *buf, size_t bytes,
  52. loff_t off);
  53. int (*mmap)(struct sysfs_open_file *of, struct vm_area_struct *vma);
  54. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  55. struct lock_class_key lockdep_key;
  56. #endif
  57. };
  58. #ifdef CONFIG_SYSFS
  59. struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
  60. const char *name, void *priv,
  61. const void *ns);
  62. struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent,
  63. const char *name,
  64. umode_t mode, loff_t size,
  65. const struct kernfs_ops *ops,
  66. void *priv, const void *ns,
  67. struct lock_class_key *key);
  68. struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
  69. const char *name,
  70. struct sysfs_dirent *target);
  71. void kernfs_remove(struct sysfs_dirent *sd);
  72. int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
  73. const void *ns);
  74. int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
  75. const char *new_name, const void *new_ns);
  76. void kernfs_enable_ns(struct sysfs_dirent *sd);
  77. int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
  78. void kernfs_notify(struct sysfs_dirent *sd);
  79. #else /* CONFIG_SYSFS */
  80. static inline struct sysfs_dirent *
  81. kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
  82. const void *ns)
  83. { return ERR_PTR(-ENOSYS); }
  84. static inline struct sysfs_dirent *
  85. kernfs_create_file_ns_key(struct sysfs_dirent *parent, const char *name,
  86. umode_t mode, loff_t size,
  87. const struct kernfs_ops *ops, void *priv,
  88. const void *ns, struct lock_class_key *key)
  89. { return ERR_PTR(-ENOSYS); }
  90. static inline struct sysfs_dirent *
  91. kernfs_create_link(struct sysfs_dirent *parent, const char *name,
  92. struct sysfs_dirent *target)
  93. { return ERR_PTR(-ENOSYS); }
  94. static inline void kernfs_remove(struct sysfs_dirent *sd) { }
  95. static inline int kernfs_remove_by_name_ns(struct sysfs_dirent *parent,
  96. const char *name, const void *ns)
  97. { return -ENOSYS; }
  98. static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
  99. struct sysfs_dirent *new_parent,
  100. const char *new_name, const void *new_ns)
  101. { return -ENOSYS; }
  102. static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { }
  103. static inline int kernfs_setattr(struct sysfs_dirent *sd,
  104. const struct iattr *iattr)
  105. { return -ENOSYS; }
  106. static inline void kernfs_notify(struct sysfs_dirent *sd) { }
  107. #endif /* CONFIG_SYSFS */
  108. static inline struct sysfs_dirent *
  109. kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv)
  110. {
  111. return kernfs_create_dir_ns(parent, name, priv, NULL);
  112. }
  113. static inline struct sysfs_dirent *
  114. kernfs_create_file_ns(struct sysfs_dirent *parent, const char *name,
  115. umode_t mode, loff_t size, const struct kernfs_ops *ops,
  116. void *priv, const void *ns)
  117. {
  118. struct lock_class_key *key = NULL;
  119. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  120. key = (struct lock_class_key *)&ops->lockdep_key;
  121. #endif
  122. return kernfs_create_file_ns_key(parent, name, mode, size, ops, priv,
  123. ns, key);
  124. }
  125. static inline struct sysfs_dirent *
  126. kernfs_create_file(struct sysfs_dirent *parent, const char *name, umode_t mode,
  127. loff_t size, const struct kernfs_ops *ops, void *priv)
  128. {
  129. return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
  130. }
  131. static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,
  132. const char *name)
  133. {
  134. return kernfs_remove_by_name_ns(parent, name, NULL);
  135. }
  136. #endif /* __LINUX_KERNFS_H */