posix_acl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 int posix_acl_valid(const struct posix_acl *);
  60. extern int posix_acl_permission(struct inode *, const struct posix_acl *, int);
  61. extern struct posix_acl *posix_acl_from_mode(mode_t, gfp_t);
  62. extern int posix_acl_equiv_mode(const struct posix_acl *, umode_t *);
  63. extern int posix_acl_create(struct posix_acl **, gfp_t, umode_t *);
  64. extern int posix_acl_chmod(struct posix_acl **, gfp_t, mode_t);
  65. extern struct posix_acl *get_posix_acl(struct inode *, int);
  66. extern int set_posix_acl(struct inode *, int, struct posix_acl *);
  67. #ifdef CONFIG_FS_POSIX_ACL
  68. static inline struct posix_acl *get_cached_acl(struct inode *inode, int type)
  69. {
  70. struct posix_acl **p, *acl;
  71. switch (type) {
  72. case ACL_TYPE_ACCESS:
  73. p = &inode->i_acl;
  74. break;
  75. case ACL_TYPE_DEFAULT:
  76. p = &inode->i_default_acl;
  77. break;
  78. default:
  79. return ERR_PTR(-EINVAL);
  80. }
  81. acl = ACCESS_ONCE(*p);
  82. if (acl) {
  83. spin_lock(&inode->i_lock);
  84. acl = *p;
  85. if (acl != ACL_NOT_CACHED)
  86. acl = posix_acl_dup(acl);
  87. spin_unlock(&inode->i_lock);
  88. }
  89. return acl;
  90. }
  91. static inline int negative_cached_acl(struct inode *inode, int type)
  92. {
  93. struct posix_acl **p, *acl;
  94. switch (type) {
  95. case ACL_TYPE_ACCESS:
  96. p = &inode->i_acl;
  97. break;
  98. case ACL_TYPE_DEFAULT:
  99. p = &inode->i_default_acl;
  100. break;
  101. default:
  102. BUG();
  103. }
  104. acl = ACCESS_ONCE(*p);
  105. if (acl)
  106. return 0;
  107. return 1;
  108. }
  109. static inline void set_cached_acl(struct inode *inode,
  110. int type,
  111. struct posix_acl *acl)
  112. {
  113. struct posix_acl *old = NULL;
  114. spin_lock(&inode->i_lock);
  115. switch (type) {
  116. case ACL_TYPE_ACCESS:
  117. old = inode->i_acl;
  118. inode->i_acl = posix_acl_dup(acl);
  119. break;
  120. case ACL_TYPE_DEFAULT:
  121. old = inode->i_default_acl;
  122. inode->i_default_acl = posix_acl_dup(acl);
  123. break;
  124. }
  125. spin_unlock(&inode->i_lock);
  126. if (old != ACL_NOT_CACHED)
  127. posix_acl_release(old);
  128. }
  129. static inline void forget_cached_acl(struct inode *inode, int type)
  130. {
  131. struct posix_acl *old = NULL;
  132. spin_lock(&inode->i_lock);
  133. switch (type) {
  134. case ACL_TYPE_ACCESS:
  135. old = inode->i_acl;
  136. inode->i_acl = ACL_NOT_CACHED;
  137. break;
  138. case ACL_TYPE_DEFAULT:
  139. old = inode->i_default_acl;
  140. inode->i_default_acl = ACL_NOT_CACHED;
  141. break;
  142. }
  143. spin_unlock(&inode->i_lock);
  144. if (old != ACL_NOT_CACHED)
  145. posix_acl_release(old);
  146. }
  147. static inline void forget_all_cached_acls(struct inode *inode)
  148. {
  149. struct posix_acl *old_access, *old_default;
  150. spin_lock(&inode->i_lock);
  151. old_access = inode->i_acl;
  152. old_default = inode->i_default_acl;
  153. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  154. spin_unlock(&inode->i_lock);
  155. if (old_access != ACL_NOT_CACHED)
  156. posix_acl_release(old_access);
  157. if (old_default != ACL_NOT_CACHED)
  158. posix_acl_release(old_default);
  159. }
  160. #endif
  161. static inline void cache_no_acl(struct inode *inode)
  162. {
  163. #ifdef CONFIG_FS_POSIX_ACL
  164. inode->i_acl = NULL;
  165. inode->i_default_acl = NULL;
  166. #endif
  167. }
  168. #endif /* __LINUX_POSIX_ACL_H */