configfs_internal.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * configfs_internal.h - Internal stuff for configfs
  4. *
  5. * Based on sysfs:
  6. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  7. *
  8. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  9. */
  10. #ifdef pr_fmt
  11. #undef pr_fmt
  12. #endif
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/slab.h>
  15. #include <linux/list.h>
  16. #include <linux/spinlock.h>
  17. struct configfs_fragment {
  18. atomic_t frag_count;
  19. struct rw_semaphore frag_sem;
  20. bool frag_dead;
  21. };
  22. void put_fragment(struct configfs_fragment *);
  23. struct configfs_fragment *get_fragment(struct configfs_fragment *);
  24. struct configfs_dirent {
  25. atomic_t s_count;
  26. int s_dependent_count;
  27. struct list_head s_sibling;
  28. struct list_head s_children;
  29. int s_links;
  30. void * s_element;
  31. int s_type;
  32. umode_t s_mode;
  33. struct dentry * s_dentry;
  34. struct iattr * s_iattr;
  35. #ifdef CONFIG_LOCKDEP
  36. int s_depth;
  37. #endif
  38. struct configfs_fragment *s_frag;
  39. };
  40. #define CONFIGFS_ROOT 0x0001
  41. #define CONFIGFS_DIR 0x0002
  42. #define CONFIGFS_ITEM_ATTR 0x0004
  43. #define CONFIGFS_ITEM_BIN_ATTR 0x0008
  44. #define CONFIGFS_ITEM_LINK 0x0020
  45. #define CONFIGFS_USET_DIR 0x0040
  46. #define CONFIGFS_USET_DEFAULT 0x0080
  47. #define CONFIGFS_USET_DROPPING 0x0100
  48. #define CONFIGFS_USET_IN_MKDIR 0x0200
  49. #define CONFIGFS_USET_CREATING 0x0400
  50. #define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)
  51. #define CONFIGFS_PINNED \
  52. (CONFIGFS_ROOT | CONFIGFS_DIR | CONFIGFS_ITEM_LINK)
  53. extern struct mutex configfs_symlink_mutex;
  54. extern spinlock_t configfs_dirent_lock;
  55. extern struct kmem_cache *configfs_dir_cachep;
  56. extern int configfs_is_root(struct config_item *item);
  57. extern struct inode * configfs_new_inode(umode_t mode, struct configfs_dirent *, struct super_block *);
  58. extern struct inode *configfs_create(struct dentry *, umode_t mode);
  59. extern int configfs_create_file(struct config_item *, const struct configfs_attribute *);
  60. extern int configfs_create_bin_file(struct config_item *,
  61. const struct configfs_bin_attribute *);
  62. extern int configfs_make_dirent(struct configfs_dirent *, struct dentry *,
  63. void *, umode_t, int, struct configfs_fragment *);
  64. extern int configfs_dirent_is_ready(struct configfs_dirent *);
  65. extern const unsigned char * configfs_get_name(struct configfs_dirent *sd);
  66. extern void configfs_drop_dentry(struct configfs_dirent *sd, struct dentry *parent);
  67. extern int configfs_setattr(struct mnt_idmap *idmap,
  68. struct dentry *dentry, struct iattr *iattr);
  69. extern struct dentry *configfs_pin_fs(void);
  70. extern void configfs_release_fs(void);
  71. extern const struct file_operations configfs_dir_operations;
  72. extern const struct file_operations configfs_file_operations;
  73. extern const struct file_operations configfs_bin_file_operations;
  74. extern const struct inode_operations configfs_dir_inode_operations;
  75. extern const struct inode_operations configfs_root_inode_operations;
  76. extern const struct inode_operations configfs_symlink_inode_operations;
  77. extern const struct dentry_operations configfs_dentry_ops;
  78. extern int configfs_symlink(struct mnt_idmap *idmap,
  79. struct inode *dir, struct dentry *dentry,
  80. const char *symname);
  81. extern int configfs_unlink(struct inode *dir, struct dentry *dentry);
  82. int configfs_create_link(struct configfs_dirent *target, struct dentry *parent,
  83. struct dentry *dentry, char *body);
  84. static inline struct config_item * to_item(struct dentry * dentry)
  85. {
  86. struct configfs_dirent * sd = dentry->d_fsdata;
  87. return ((struct config_item *) sd->s_element);
  88. }
  89. static inline struct configfs_attribute * to_attr(struct dentry * dentry)
  90. {
  91. struct configfs_dirent * sd = dentry->d_fsdata;
  92. return ((struct configfs_attribute *) sd->s_element);
  93. }
  94. static inline struct configfs_bin_attribute *to_bin_attr(struct dentry *dentry)
  95. {
  96. struct configfs_attribute *attr = to_attr(dentry);
  97. return container_of(attr, struct configfs_bin_attribute, cb_attr);
  98. }
  99. static inline struct config_item *configfs_get_config_item(struct dentry *dentry)
  100. {
  101. struct config_item * item = NULL;
  102. spin_lock(&dentry->d_lock);
  103. if (!d_unhashed(dentry)) {
  104. struct configfs_dirent * sd = dentry->d_fsdata;
  105. item = config_item_get(sd->s_element);
  106. }
  107. spin_unlock(&dentry->d_lock);
  108. return item;
  109. }
  110. static inline void release_configfs_dirent(struct configfs_dirent * sd)
  111. {
  112. if (!(sd->s_type & CONFIGFS_ROOT)) {
  113. kfree(sd->s_iattr);
  114. put_fragment(sd->s_frag);
  115. kmem_cache_free(configfs_dir_cachep, sd);
  116. }
  117. }
  118. static inline struct configfs_dirent * configfs_get(struct configfs_dirent * sd)
  119. {
  120. if (sd) {
  121. WARN_ON(!atomic_read(&sd->s_count));
  122. atomic_inc(&sd->s_count);
  123. }
  124. return sd;
  125. }
  126. static inline void configfs_put(struct configfs_dirent * sd)
  127. {
  128. WARN_ON(!atomic_read(&sd->s_count));
  129. if (atomic_dec_and_test(&sd->s_count))
  130. release_configfs_dirent(sd);
  131. }