posix_acl.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/slab.h>
  8. #define ACL_UNDEFINED_ID (-1)
  9. /* a_type field in acl_user_posix_entry_t */
  10. #define ACL_TYPE_ACCESS (0x8000)
  11. #define ACL_TYPE_DEFAULT (0x4000)
  12. /* e_tag entry in struct posix_acl_entry */
  13. #define ACL_USER_OBJ (0x01)
  14. #define ACL_USER (0x02)
  15. #define ACL_GROUP_OBJ (0x04)
  16. #define ACL_GROUP (0x08)
  17. #define ACL_MASK (0x10)
  18. #define ACL_OTHER (0x20)
  19. /* permissions in the e_perm field */
  20. #define ACL_READ (0x04)
  21. #define ACL_WRITE (0x02)
  22. #define ACL_EXECUTE (0x01)
  23. //#define ACL_ADD (0x08)
  24. //#define ACL_DELETE (0x10)
  25. struct posix_acl_entry {
  26. short e_tag;
  27. unsigned short e_perm;
  28. unsigned int e_id;
  29. };
  30. struct posix_acl {
  31. atomic_t a_refcount;
  32. unsigned int a_count;
  33. struct posix_acl_entry a_entries[0];
  34. };
  35. #define FOREACH_ACL_ENTRY(pa, acl, pe) \
  36. for(pa=(acl)->a_entries, pe=pa+(acl)->a_count; pa<pe; pa++)
  37. /*
  38. * Duplicate an ACL handle.
  39. */
  40. static inline struct posix_acl *
  41. posix_acl_dup(struct posix_acl *acl)
  42. {
  43. if (acl)
  44. atomic_inc(&acl->a_refcount);
  45. return acl;
  46. }
  47. /*
  48. * Free an ACL handle.
  49. */
  50. static inline void
  51. posix_acl_release(struct posix_acl *acl)
  52. {
  53. if (acl && atomic_dec_and_test(&acl->a_refcount))
  54. kfree(acl);
  55. }
  56. /* posix_acl.c */
  57. extern void posix_acl_init(struct posix_acl *, int);
  58. extern struct posix_acl *posix_acl_alloc(int, gfp_t);
  59. extern struct posix_acl *posix_acl_clone(const struct posix_acl *, gfp_t);
  60. extern int posix_acl_valid(const struct posix_acl *);
  61. extern int posix_acl_permission(struct inode *, const struct posix_acl *, int);
  62. extern struct posix_acl *posix_acl_from_mode(mode_t, gfp_t);
  63. extern int posix_acl_equiv_mode(const struct posix_acl *, mode_t *);
  64. extern int posix_acl_create_masq(struct posix_acl *, mode_t *);
  65. extern int posix_acl_chmod_masq(struct posix_acl *, mode_t);
  66. extern struct posix_acl *get_posix_acl(struct inode *, int);
  67. extern int set_posix_acl(struct inode *, int, struct posix_acl *);
  68. #ifdef CONFIG_FS_POSIX_ACL
  69. static inline struct posix_acl *get_cached_acl(struct inode *inode, int type)
  70. {
  71. struct posix_acl **p, *acl;
  72. switch (type) {
  73. case ACL_TYPE_ACCESS:
  74. p = &inode->i_acl;
  75. break;
  76. case ACL_TYPE_DEFAULT:
  77. p = &inode->i_default_acl;
  78. break;
  79. default:
  80. return ERR_PTR(-EINVAL);
  81. }
  82. acl = ACCESS_ONCE(*p);
  83. if (acl) {
  84. spin_lock(&inode->i_lock);
  85. acl = *p;
  86. if (acl != ACL_NOT_CACHED)
  87. acl = posix_acl_dup(acl);
  88. spin_unlock(&inode->i_lock);
  89. }
  90. return acl;
  91. }
  92. static inline int negative_cached_acl(struct inode *inode, int type)
  93. {
  94. struct posix_acl **p, *acl;
  95. switch (type) {
  96. case ACL_TYPE_ACCESS:
  97. p = &inode->i_acl;
  98. break;
  99. case ACL_TYPE_DEFAULT:
  100. p = &inode->i_default_acl;
  101. break;
  102. default:
  103. BUG();
  104. }
  105. acl = ACCESS_ONCE(*p);
  106. if (acl)
  107. return 0;
  108. return 1;
  109. }
  110. static inline void set_cached_acl(struct inode *inode,
  111. int type,
  112. struct posix_acl *acl)
  113. {
  114. struct posix_acl *old = NULL;
  115. spin_lock(&inode->i_lock);
  116. switch (type) {
  117. case ACL_TYPE_ACCESS:
  118. old = inode->i_acl;
  119. inode->i_acl = posix_acl_dup(acl);
  120. break;
  121. case ACL_TYPE_DEFAULT:
  122. old = inode->i_default_acl;
  123. inode->i_default_acl = posix_acl_dup(acl);
  124. break;
  125. }
  126. spin_unlock(&inode->i_lock);
  127. if (old != ACL_NOT_CACHED)
  128. posix_acl_release(old);
  129. }
  130. static inline void forget_cached_acl(struct inode *inode, int type)
  131. {
  132. struct posix_acl *old = NULL;
  133. spin_lock(&inode->i_lock);
  134. switch (type) {
  135. case ACL_TYPE_ACCESS:
  136. old = inode->i_acl;
  137. inode->i_acl = ACL_NOT_CACHED;
  138. break;
  139. case ACL_TYPE_DEFAULT:
  140. old = inode->i_default_acl;
  141. inode->i_default_acl = ACL_NOT_CACHED;
  142. break;
  143. }
  144. spin_unlock(&inode->i_lock);
  145. if (old != ACL_NOT_CACHED)
  146. posix_acl_release(old);
  147. }
  148. static inline void forget_all_cached_acls(struct inode *inode)
  149. {
  150. struct posix_acl *old_access, *old_default;
  151. spin_lock(&inode->i_lock);
  152. old_access = inode->i_acl;
  153. old_default = inode->i_default_acl;
  154. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  155. spin_unlock(&inode->i_lock);
  156. if (old_access != ACL_NOT_CACHED)
  157. posix_acl_release(old_access);
  158. if (old_default != ACL_NOT_CACHED)
  159. posix_acl_release(old_default);
  160. }
  161. #endif
  162. static inline void cache_no_acl(struct inode *inode)
  163. {
  164. #ifdef CONFIG_FS_POSIX_ACL
  165. inode->i_acl = NULL;
  166. inode->i_default_acl = NULL;
  167. #endif
  168. }
  169. #endif /* __LINUX_POSIX_ACL_H */