posix_acl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 int posix_acl_update_mode(struct inode *, umode_t *, struct posix_acl **);
  71. extern struct posix_acl *get_posix_acl(struct inode *, int);
  72. extern int set_posix_acl(struct inode *, int, struct posix_acl *);
  73. #ifdef CONFIG_FS_POSIX_ACL
  74. static inline struct posix_acl **acl_by_type(struct inode *inode, int type)
  75. {
  76. switch (type) {
  77. case ACL_TYPE_ACCESS:
  78. return &inode->i_acl;
  79. case ACL_TYPE_DEFAULT:
  80. return &inode->i_default_acl;
  81. default:
  82. BUG();
  83. }
  84. }
  85. static inline struct posix_acl *get_cached_acl(struct inode *inode, int type)
  86. {
  87. struct posix_acl **p = acl_by_type(inode, type);
  88. struct posix_acl *acl = ACCESS_ONCE(*p);
  89. if (acl) {
  90. spin_lock(&inode->i_lock);
  91. acl = *p;
  92. if (acl != ACL_NOT_CACHED)
  93. acl = posix_acl_dup(acl);
  94. spin_unlock(&inode->i_lock);
  95. }
  96. return acl;
  97. }
  98. static inline struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
  99. {
  100. return rcu_dereference(*acl_by_type(inode, type));
  101. }
  102. static inline void set_cached_acl(struct inode *inode,
  103. int type,
  104. struct posix_acl *acl)
  105. {
  106. struct posix_acl **p = acl_by_type(inode, type);
  107. struct posix_acl *old;
  108. spin_lock(&inode->i_lock);
  109. old = *p;
  110. rcu_assign_pointer(*p, posix_acl_dup(acl));
  111. spin_unlock(&inode->i_lock);
  112. if (old != ACL_NOT_CACHED)
  113. posix_acl_release(old);
  114. }
  115. static inline void forget_cached_acl(struct inode *inode, int type)
  116. {
  117. struct posix_acl **p = acl_by_type(inode, type);
  118. struct posix_acl *old;
  119. spin_lock(&inode->i_lock);
  120. old = *p;
  121. *p = ACL_NOT_CACHED;
  122. spin_unlock(&inode->i_lock);
  123. if (old != ACL_NOT_CACHED)
  124. posix_acl_release(old);
  125. }
  126. static inline void forget_all_cached_acls(struct inode *inode)
  127. {
  128. struct posix_acl *old_access, *old_default;
  129. spin_lock(&inode->i_lock);
  130. old_access = inode->i_acl;
  131. old_default = inode->i_default_acl;
  132. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  133. spin_unlock(&inode->i_lock);
  134. if (old_access != ACL_NOT_CACHED)
  135. posix_acl_release(old_access);
  136. if (old_default != ACL_NOT_CACHED)
  137. posix_acl_release(old_default);
  138. }
  139. #endif
  140. static inline void cache_no_acl(struct inode *inode)
  141. {
  142. #ifdef CONFIG_FS_POSIX_ACL
  143. inode->i_acl = NULL;
  144. inode->i_default_acl = NULL;
  145. #endif
  146. }
  147. #endif /* __LINUX_POSIX_ACL_H */