posix_acl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. File: linux/posix_acl.h
  3. (C) 2002 Andreas Gruenbacher, <a.gruenbacher@computer.org>
  4. */
  5. #ifndef __LINUX_POSIX_ACL_H
  6. #define __LINUX_POSIX_ACL_H
  7. #include <linux/bug.h>
  8. #include <linux/slab.h>
  9. #include <linux/rcupdate.h>
  10. #define ACL_UNDEFINED_ID (-1)
  11. /* a_type field in acl_user_posix_entry_t */
  12. #define ACL_TYPE_ACCESS (0x8000)
  13. #define ACL_TYPE_DEFAULT (0x4000)
  14. /* e_tag entry in struct posix_acl_entry */
  15. #define ACL_USER_OBJ (0x01)
  16. #define ACL_USER (0x02)
  17. #define ACL_GROUP_OBJ (0x04)
  18. #define ACL_GROUP (0x08)
  19. #define ACL_MASK (0x10)
  20. #define ACL_OTHER (0x20)
  21. /* permissions in the e_perm field */
  22. #define ACL_READ (0x04)
  23. #define ACL_WRITE (0x02)
  24. #define ACL_EXECUTE (0x01)
  25. //#define ACL_ADD (0x08)
  26. //#define ACL_DELETE (0x10)
  27. struct posix_acl_entry {
  28. short e_tag;
  29. unsigned short e_perm;
  30. unsigned int e_id;
  31. };
  32. struct posix_acl {
  33. union {
  34. atomic_t a_refcount;
  35. struct rcu_head a_rcu;
  36. };
  37. unsigned int a_count;
  38. struct posix_acl_entry a_entries[0];
  39. };
  40. #define FOREACH_ACL_ENTRY(pa, acl, pe) \
  41. for(pa=(acl)->a_entries, pe=pa+(acl)->a_count; pa<pe; pa++)
  42. /*
  43. * Duplicate an ACL handle.
  44. */
  45. static inline struct posix_acl *
  46. posix_acl_dup(struct posix_acl *acl)
  47. {
  48. if (acl)
  49. atomic_inc(&acl->a_refcount);
  50. return acl;
  51. }
  52. /*
  53. * Free an ACL handle.
  54. */
  55. static inline void
  56. posix_acl_release(struct posix_acl *acl)
  57. {
  58. if (acl && atomic_dec_and_test(&acl->a_refcount))
  59. kfree_rcu(acl, a_rcu);
  60. }
  61. /* posix_acl.c */
  62. extern void posix_acl_init(struct posix_acl *, int);
  63. extern struct posix_acl *posix_acl_alloc(int, gfp_t);
  64. extern int posix_acl_valid(const struct posix_acl *);
  65. extern int posix_acl_permission(struct inode *, const struct posix_acl *, int);
  66. extern struct posix_acl *posix_acl_from_mode(umode_t, gfp_t);
  67. extern int posix_acl_equiv_mode(const struct posix_acl *, umode_t *);
  68. extern int posix_acl_create(struct posix_acl **, gfp_t, umode_t *);
  69. extern int posix_acl_chmod(struct posix_acl **, gfp_t, umode_t);
  70. extern struct posix_acl *get_posix_acl(struct inode *, int);
  71. extern int set_posix_acl(struct inode *, int, struct posix_acl *);
  72. #ifdef CONFIG_FS_POSIX_ACL
  73. static inline struct posix_acl **acl_by_type(struct inode *inode, int type)
  74. {
  75. switch (type) {
  76. case ACL_TYPE_ACCESS:
  77. return &inode->i_acl;
  78. case ACL_TYPE_DEFAULT:
  79. return &inode->i_default_acl;
  80. default:
  81. BUG();
  82. }
  83. }
  84. static inline struct posix_acl *get_cached_acl(struct inode *inode, int type)
  85. {
  86. struct posix_acl **p = acl_by_type(inode, type);
  87. struct posix_acl *acl = ACCESS_ONCE(*p);
  88. if (acl) {
  89. spin_lock(&inode->i_lock);
  90. acl = *p;
  91. if (acl != ACL_NOT_CACHED)
  92. acl = posix_acl_dup(acl);
  93. spin_unlock(&inode->i_lock);
  94. }
  95. return acl;
  96. }
  97. static inline struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
  98. {
  99. return rcu_dereference(*acl_by_type(inode, type));
  100. }
  101. static inline void set_cached_acl(struct inode *inode,
  102. int type,
  103. struct posix_acl *acl)
  104. {
  105. struct posix_acl **p = acl_by_type(inode, type);
  106. struct posix_acl *old;
  107. spin_lock(&inode->i_lock);
  108. old = *p;
  109. rcu_assign_pointer(*p, posix_acl_dup(acl));
  110. spin_unlock(&inode->i_lock);
  111. if (old != ACL_NOT_CACHED)
  112. posix_acl_release(old);
  113. }
  114. static inline void forget_cached_acl(struct inode *inode, int type)
  115. {
  116. struct posix_acl **p = acl_by_type(inode, type);
  117. struct posix_acl *old;
  118. spin_lock(&inode->i_lock);
  119. old = *p;
  120. *p = ACL_NOT_CACHED;
  121. spin_unlock(&inode->i_lock);
  122. if (old != ACL_NOT_CACHED)
  123. posix_acl_release(old);
  124. }
  125. static inline void forget_all_cached_acls(struct inode *inode)
  126. {
  127. struct posix_acl *old_access, *old_default;
  128. spin_lock(&inode->i_lock);
  129. old_access = inode->i_acl;
  130. old_default = inode->i_default_acl;
  131. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  132. spin_unlock(&inode->i_lock);
  133. if (old_access != ACL_NOT_CACHED)
  134. posix_acl_release(old_access);
  135. if (old_default != ACL_NOT_CACHED)
  136. posix_acl_release(old_default);
  137. }
  138. #endif
  139. static inline void cache_no_acl(struct inode *inode)
  140. {
  141. #ifdef CONFIG_FS_POSIX_ACL
  142. inode->i_acl = NULL;
  143. inode->i_default_acl = NULL;
  144. #endif
  145. }
  146. #endif /* __LINUX_POSIX_ACL_H */