xfs_acl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright (c) 2008, Christoph Hellwig
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_acl.h"
  20. #include "xfs_attr.h"
  21. #include "xfs_bmap_btree.h"
  22. #include "xfs_inode.h"
  23. #include "xfs_vnodeops.h"
  24. #include "xfs_trace.h"
  25. #include <linux/slab.h>
  26. #include <linux/xattr.h>
  27. #include <linux/posix_acl_xattr.h>
  28. /*
  29. * Locking scheme:
  30. * - all ACL updates are protected by inode->i_mutex, which is taken before
  31. * calling into this file.
  32. */
  33. STATIC struct posix_acl *
  34. xfs_acl_from_disk(struct xfs_acl *aclp)
  35. {
  36. struct posix_acl_entry *acl_e;
  37. struct posix_acl *acl;
  38. struct xfs_acl_entry *ace;
  39. int count, i;
  40. count = be32_to_cpu(aclp->acl_cnt);
  41. acl = posix_acl_alloc(count, GFP_KERNEL);
  42. if (!acl)
  43. return ERR_PTR(-ENOMEM);
  44. for (i = 0; i < count; i++) {
  45. acl_e = &acl->a_entries[i];
  46. ace = &aclp->acl_entry[i];
  47. /*
  48. * The tag is 32 bits on disk and 16 bits in core.
  49. *
  50. * Because every access to it goes through the core
  51. * format first this is not a problem.
  52. */
  53. acl_e->e_tag = be32_to_cpu(ace->ae_tag);
  54. acl_e->e_perm = be16_to_cpu(ace->ae_perm);
  55. switch (acl_e->e_tag) {
  56. case ACL_USER:
  57. case ACL_GROUP:
  58. acl_e->e_id = be32_to_cpu(ace->ae_id);
  59. break;
  60. case ACL_USER_OBJ:
  61. case ACL_GROUP_OBJ:
  62. case ACL_MASK:
  63. case ACL_OTHER:
  64. acl_e->e_id = ACL_UNDEFINED_ID;
  65. break;
  66. default:
  67. goto fail;
  68. }
  69. }
  70. return acl;
  71. fail:
  72. posix_acl_release(acl);
  73. return ERR_PTR(-EINVAL);
  74. }
  75. STATIC void
  76. xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
  77. {
  78. const struct posix_acl_entry *acl_e;
  79. struct xfs_acl_entry *ace;
  80. int i;
  81. aclp->acl_cnt = cpu_to_be32(acl->a_count);
  82. for (i = 0; i < acl->a_count; i++) {
  83. ace = &aclp->acl_entry[i];
  84. acl_e = &acl->a_entries[i];
  85. ace->ae_tag = cpu_to_be32(acl_e->e_tag);
  86. ace->ae_id = cpu_to_be32(acl_e->e_id);
  87. ace->ae_perm = cpu_to_be16(acl_e->e_perm);
  88. }
  89. }
  90. struct posix_acl *
  91. xfs_get_acl(struct inode *inode, int type)
  92. {
  93. struct xfs_inode *ip = XFS_I(inode);
  94. struct posix_acl *acl;
  95. struct xfs_acl *xfs_acl;
  96. int len = sizeof(struct xfs_acl);
  97. unsigned char *ea_name;
  98. int error;
  99. acl = get_cached_acl(inode, type);
  100. if (acl != ACL_NOT_CACHED)
  101. return acl;
  102. trace_xfs_get_acl(ip);
  103. switch (type) {
  104. case ACL_TYPE_ACCESS:
  105. ea_name = SGI_ACL_FILE;
  106. break;
  107. case ACL_TYPE_DEFAULT:
  108. ea_name = SGI_ACL_DEFAULT;
  109. break;
  110. default:
  111. BUG();
  112. }
  113. /*
  114. * If we have a cached ACLs value just return it, not need to
  115. * go out to the disk.
  116. */
  117. xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
  118. if (!xfs_acl)
  119. return ERR_PTR(-ENOMEM);
  120. error = -xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
  121. &len, ATTR_ROOT);
  122. if (error) {
  123. /*
  124. * If the attribute doesn't exist make sure we have a negative
  125. * cache entry, for any other error assume it is transient and
  126. * leave the cache entry as ACL_NOT_CACHED.
  127. */
  128. if (error == -ENOATTR) {
  129. acl = NULL;
  130. goto out_update_cache;
  131. }
  132. goto out;
  133. }
  134. acl = xfs_acl_from_disk(xfs_acl);
  135. if (IS_ERR(acl))
  136. goto out;
  137. out_update_cache:
  138. set_cached_acl(inode, type, acl);
  139. out:
  140. kfree(xfs_acl);
  141. return acl;
  142. }
  143. STATIC int
  144. xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  145. {
  146. struct xfs_inode *ip = XFS_I(inode);
  147. unsigned char *ea_name;
  148. int error;
  149. if (S_ISLNK(inode->i_mode))
  150. return -EOPNOTSUPP;
  151. switch (type) {
  152. case ACL_TYPE_ACCESS:
  153. ea_name = SGI_ACL_FILE;
  154. break;
  155. case ACL_TYPE_DEFAULT:
  156. if (!S_ISDIR(inode->i_mode))
  157. return acl ? -EACCES : 0;
  158. ea_name = SGI_ACL_DEFAULT;
  159. break;
  160. default:
  161. return -EINVAL;
  162. }
  163. if (acl) {
  164. struct xfs_acl *xfs_acl;
  165. int len;
  166. xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
  167. if (!xfs_acl)
  168. return -ENOMEM;
  169. xfs_acl_to_disk(xfs_acl, acl);
  170. len = sizeof(struct xfs_acl) -
  171. (sizeof(struct xfs_acl_entry) *
  172. (XFS_ACL_MAX_ENTRIES - acl->a_count));
  173. error = -xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
  174. len, ATTR_ROOT);
  175. kfree(xfs_acl);
  176. } else {
  177. /*
  178. * A NULL ACL argument means we want to remove the ACL.
  179. */
  180. error = -xfs_attr_remove(ip, ea_name, ATTR_ROOT);
  181. /*
  182. * If the attribute didn't exist to start with that's fine.
  183. */
  184. if (error == -ENOATTR)
  185. error = 0;
  186. }
  187. if (!error)
  188. set_cached_acl(inode, type, acl);
  189. return error;
  190. }
  191. static int
  192. xfs_set_mode(struct inode *inode, umode_t mode)
  193. {
  194. int error = 0;
  195. if (mode != inode->i_mode) {
  196. struct iattr iattr;
  197. iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
  198. iattr.ia_mode = mode;
  199. iattr.ia_ctime = current_fs_time(inode->i_sb);
  200. error = -xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  201. }
  202. return error;
  203. }
  204. static int
  205. xfs_acl_exists(struct inode *inode, unsigned char *name)
  206. {
  207. int len = sizeof(struct xfs_acl);
  208. return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
  209. ATTR_ROOT|ATTR_KERNOVAL) == 0);
  210. }
  211. int
  212. posix_acl_access_exists(struct inode *inode)
  213. {
  214. return xfs_acl_exists(inode, SGI_ACL_FILE);
  215. }
  216. int
  217. posix_acl_default_exists(struct inode *inode)
  218. {
  219. if (!S_ISDIR(inode->i_mode))
  220. return 0;
  221. return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
  222. }
  223. /*
  224. * No need for i_mutex because the inode is not yet exposed to the VFS.
  225. */
  226. int
  227. xfs_inherit_acl(struct inode *inode, struct posix_acl *acl)
  228. {
  229. umode_t mode = inode->i_mode;
  230. int error = 0, inherit = 0;
  231. if (S_ISDIR(inode->i_mode)) {
  232. error = xfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  233. if (error)
  234. goto out;
  235. }
  236. error = posix_acl_create(&acl, GFP_KERNEL, &mode);
  237. if (error < 0)
  238. return error;
  239. /*
  240. * If posix_acl_create returns a positive value we need to
  241. * inherit a permission that can't be represented using the Unix
  242. * mode bits and we actually need to set an ACL.
  243. */
  244. if (error > 0)
  245. inherit = 1;
  246. error = xfs_set_mode(inode, mode);
  247. if (error)
  248. goto out;
  249. if (inherit)
  250. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, acl);
  251. out:
  252. posix_acl_release(acl);
  253. return error;
  254. }
  255. int
  256. xfs_acl_chmod(struct inode *inode)
  257. {
  258. struct posix_acl *acl;
  259. int error;
  260. if (S_ISLNK(inode->i_mode))
  261. return -EOPNOTSUPP;
  262. acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
  263. if (IS_ERR(acl) || !acl)
  264. return PTR_ERR(acl);
  265. error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  266. if (error)
  267. return error;
  268. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, acl);
  269. posix_acl_release(acl);
  270. return error;
  271. }
  272. static int
  273. xfs_xattr_acl_get(struct dentry *dentry, const char *name,
  274. void *value, size_t size, int type)
  275. {
  276. struct posix_acl *acl;
  277. int error;
  278. acl = xfs_get_acl(dentry->d_inode, type);
  279. if (IS_ERR(acl))
  280. return PTR_ERR(acl);
  281. if (acl == NULL)
  282. return -ENODATA;
  283. error = posix_acl_to_xattr(acl, value, size);
  284. posix_acl_release(acl);
  285. return error;
  286. }
  287. static int
  288. xfs_xattr_acl_set(struct dentry *dentry, const char *name,
  289. const void *value, size_t size, int flags, int type)
  290. {
  291. struct inode *inode = dentry->d_inode;
  292. struct posix_acl *acl = NULL;
  293. int error = 0;
  294. if (flags & XATTR_CREATE)
  295. return -EINVAL;
  296. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  297. return value ? -EACCES : 0;
  298. if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
  299. return -EPERM;
  300. if (!value)
  301. goto set_acl;
  302. acl = posix_acl_from_xattr(value, size);
  303. if (!acl) {
  304. /*
  305. * acl_set_file(3) may request that we set default ACLs with
  306. * zero length -- defend (gracefully) against that here.
  307. */
  308. goto out;
  309. }
  310. if (IS_ERR(acl)) {
  311. error = PTR_ERR(acl);
  312. goto out;
  313. }
  314. error = posix_acl_valid(acl);
  315. if (error)
  316. goto out_release;
  317. error = -EINVAL;
  318. if (acl->a_count > XFS_ACL_MAX_ENTRIES)
  319. goto out_release;
  320. if (type == ACL_TYPE_ACCESS) {
  321. umode_t mode = inode->i_mode;
  322. error = posix_acl_equiv_mode(acl, &mode);
  323. if (error <= 0) {
  324. posix_acl_release(acl);
  325. acl = NULL;
  326. if (error < 0)
  327. return error;
  328. }
  329. error = xfs_set_mode(inode, mode);
  330. if (error)
  331. goto out_release;
  332. }
  333. set_acl:
  334. error = xfs_set_acl(inode, type, acl);
  335. out_release:
  336. posix_acl_release(acl);
  337. out:
  338. return error;
  339. }
  340. const struct xattr_handler xfs_xattr_acl_access_handler = {
  341. .prefix = POSIX_ACL_XATTR_ACCESS,
  342. .flags = ACL_TYPE_ACCESS,
  343. .get = xfs_xattr_acl_get,
  344. .set = xfs_xattr_acl_set,
  345. };
  346. const struct xattr_handler xfs_xattr_acl_default_handler = {
  347. .prefix = POSIX_ACL_XATTR_DEFAULT,
  348. .flags = ACL_TYPE_DEFAULT,
  349. .get = xfs_xattr_acl_get,
  350. .set = xfs_xattr_acl_set,
  351. };