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. extern struct mutex configfs_symlink_mutex;
  52. extern spinlock_t configfs_dirent_lock;
  53. extern struct kmem_cache *configfs_dir_cachep;
  54. extern int configfs_is_root(struct config_item *item);
  55. extern struct inode * configfs_new_inode(umode_t mode, struct configfs_dirent *, struct super_block *);
  56. extern struct inode *configfs_create(struct dentry *, umode_t mode);
  57. extern int configfs_create_file(struct config_item *, const struct configfs_attribute *);
  58. extern int configfs_create_bin_file(struct config_item *,
  59. const struct configfs_bin_attribute *);
  60. extern int configfs_make_dirent(struct configfs_dirent *, struct dentry *,
  61. void *, umode_t, int, struct configfs_fragment *);
  62. extern int configfs_dirent_is_ready(struct configfs_dirent *);
  63. extern void configfs_hash_and_remove(struct dentry * dir, const char * name);
  64. extern const unsigned char * configfs_get_name(struct configfs_dirent *sd);
  65. extern void configfs_drop_dentry(struct configfs_dirent *sd, struct dentry *parent);
  66. extern int configfs_setattr(struct user_namespace *mnt_userns,
  67. struct dentry *dentry, struct iattr *iattr);
  68. extern struct dentry *configfs_pin_fs(void);
  69. extern void configfs_release_fs(void);
  70. extern const struct file_operations configfs_dir_operations;
  71. extern const struct file_operations configfs_file_operations;
  72. extern const struct file_operations configfs_bin_file_operations;
  73. extern const struct inode_operations configfs_dir_inode_operations;
  74. extern const struct inode_operations configfs_root_inode_operations;
  75. extern const struct inode_operations configfs_symlink_inode_operations;
  76. extern const struct dentry_operations configfs_dentry_ops;
  77. extern int configfs_symlink(struct user_namespace *mnt_userns,
  78. struct inode *dir, struct dentry *dentry,
  79. const char *symname);
  80. extern int configfs_unlink(struct inode *dir, struct dentry *dentry);
  81. int configfs_create_link(struct configfs_dirent *target, struct dentry *parent,
  82. struct dentry *dentry, char *body);
  83. static inline struct config_item * to_item(struct dentry * dentry)
  84. {
  85. struct configfs_dirent * sd = dentry->d_fsdata;
  86. return ((struct config_item *) sd->s_element);
  87. }
  88. static inline struct configfs_attribute * to_attr(struct dentry * dentry)
  89. {
  90. struct configfs_dirent * sd = dentry->d_fsdata;
  91. return ((struct configfs_attribute *) sd->s_element);
  92. }
  93. static inline struct configfs_bin_attribute *to_bin_attr(struct dentry *dentry)
  94. {
  95. struct configfs_attribute *attr = to_attr(dentry);
  96. return container_of(attr, struct configfs_bin_attribute, cb_attr);
  97. }
  98. static inline struct config_item *configfs_get_config_item(struct dentry *dentry)
  99. {
  100. struct config_item * item = NULL;
  101. spin_lock(&dentry->d_lock);
  102. if (!d_unhashed(dentry)) {
  103. struct configfs_dirent * sd = dentry->d_fsdata;
  104. item = config_item_get(sd->s_element);
  105. }
  106. spin_unlock(&dentry->d_lock);
  107. return item;
  108. }
  109. static inline void release_configfs_dirent(struct configfs_dirent * sd)
  110. {
  111. if (!(sd->s_type & CONFIGFS_ROOT)) {
  112. kfree(sd->s_iattr);
  113. put_fragment(sd->s_frag);
  114. kmem_cache_free(configfs_dir_cachep, sd);
  115. }
  116. }
  117. static inline struct configfs_dirent * configfs_get(struct configfs_dirent * sd)
  118. {
  119. if (sd) {
  120. WARN_ON(!atomic_read(&sd->s_count));
  121. atomic_inc(&sd->s_count);
  122. }
  123. return sd;
  124. }
  125. static inline void configfs_put(struct configfs_dirent * sd)
  126. {
  127. WARN_ON(!atomic_read(&sd->s_count));
  128. if (atomic_dec_and_test(&sd->s_count))
  129. release_configfs_dirent(sd);
  130. }