xfs_acl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/xattr.h>
  26. #include <linux/posix_acl_xattr.h>
  27. /*
  28. * Locking scheme:
  29. * - all ACL updates are protected by inode->i_mutex, which is taken before
  30. * calling into this file.
  31. */
  32. STATIC struct posix_acl *
  33. xfs_acl_from_disk(struct xfs_acl *aclp)
  34. {
  35. struct posix_acl_entry *acl_e;
  36. struct posix_acl *acl;
  37. struct xfs_acl_entry *ace;
  38. int count, i;
  39. count = be32_to_cpu(aclp->acl_cnt);
  40. acl = posix_acl_alloc(count, GFP_KERNEL);
  41. if (!acl)
  42. return ERR_PTR(-ENOMEM);
  43. for (i = 0; i < count; i++) {
  44. acl_e = &acl->a_entries[i];
  45. ace = &aclp->acl_entry[i];
  46. /*
  47. * The tag is 32 bits on disk and 16 bits in core.
  48. *
  49. * Because every access to it goes through the core
  50. * format first this is not a problem.
  51. */
  52. acl_e->e_tag = be32_to_cpu(ace->ae_tag);
  53. acl_e->e_perm = be16_to_cpu(ace->ae_perm);
  54. switch (acl_e->e_tag) {
  55. case ACL_USER:
  56. case ACL_GROUP:
  57. acl_e->e_id = be32_to_cpu(ace->ae_id);
  58. break;
  59. case ACL_USER_OBJ:
  60. case ACL_GROUP_OBJ:
  61. case ACL_MASK:
  62. case ACL_OTHER:
  63. acl_e->e_id = ACL_UNDEFINED_ID;
  64. break;
  65. default:
  66. goto fail;
  67. }
  68. }
  69. return acl;
  70. fail:
  71. posix_acl_release(acl);
  72. return ERR_PTR(-EINVAL);
  73. }
  74. STATIC void
  75. xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
  76. {
  77. const struct posix_acl_entry *acl_e;
  78. struct xfs_acl_entry *ace;
  79. int i;
  80. aclp->acl_cnt = cpu_to_be32(acl->a_count);
  81. for (i = 0; i < acl->a_count; i++) {
  82. ace = &aclp->acl_entry[i];
  83. acl_e = &acl->a_entries[i];
  84. ace->ae_tag = cpu_to_be32(acl_e->e_tag);
  85. ace->ae_id = cpu_to_be32(acl_e->e_id);
  86. ace->ae_perm = cpu_to_be16(acl_e->e_perm);
  87. }
  88. }
  89. struct posix_acl *
  90. xfs_get_acl(struct inode *inode, int type)
  91. {
  92. struct xfs_inode *ip = XFS_I(inode);
  93. struct posix_acl *acl;
  94. struct xfs_acl *xfs_acl;
  95. int len = sizeof(struct xfs_acl);
  96. char *ea_name;
  97. int error;
  98. acl = get_cached_acl(inode, type);
  99. if (acl != ACL_NOT_CACHED)
  100. return acl;
  101. switch (type) {
  102. case ACL_TYPE_ACCESS:
  103. ea_name = SGI_ACL_FILE;
  104. break;
  105. case ACL_TYPE_DEFAULT:
  106. ea_name = SGI_ACL_DEFAULT;
  107. break;
  108. default:
  109. BUG();
  110. }
  111. /*
  112. * If we have a cached ACLs value just return it, not need to
  113. * go out to the disk.
  114. */
  115. xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
  116. if (!xfs_acl)
  117. return ERR_PTR(-ENOMEM);
  118. error = -xfs_attr_get(ip, ea_name, (char *)xfs_acl, &len, ATTR_ROOT);
  119. if (error) {
  120. /*
  121. * If the attribute doesn't exist make sure we have a negative
  122. * cache entry, for any other error assume it is transient and
  123. * leave the cache entry as ACL_NOT_CACHED.
  124. */
  125. if (error == -ENOATTR) {
  126. acl = NULL;
  127. goto out_update_cache;
  128. }
  129. goto out;
  130. }
  131. acl = xfs_acl_from_disk(xfs_acl);
  132. if (IS_ERR(acl))
  133. goto out;
  134. out_update_cache:
  135. set_cached_acl(inode, type, acl);
  136. out:
  137. kfree(xfs_acl);
  138. return acl;
  139. }
  140. STATIC int
  141. xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  142. {
  143. struct xfs_inode *ip = XFS_I(inode);
  144. char *ea_name;
  145. int error;
  146. if (S_ISLNK(inode->i_mode))
  147. return -EOPNOTSUPP;
  148. switch (type) {
  149. case ACL_TYPE_ACCESS:
  150. ea_name = SGI_ACL_FILE;
  151. break;
  152. case ACL_TYPE_DEFAULT:
  153. if (!S_ISDIR(inode->i_mode))
  154. return acl ? -EACCES : 0;
  155. ea_name = SGI_ACL_DEFAULT;
  156. break;
  157. default:
  158. return -EINVAL;
  159. }
  160. if (acl) {
  161. struct xfs_acl *xfs_acl;
  162. int len;
  163. xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
  164. if (!xfs_acl)
  165. return -ENOMEM;
  166. xfs_acl_to_disk(xfs_acl, acl);
  167. len = sizeof(struct xfs_acl) -
  168. (sizeof(struct xfs_acl_entry) *
  169. (XFS_ACL_MAX_ENTRIES - acl->a_count));
  170. error = -xfs_attr_set(ip, ea_name, (char *)xfs_acl,
  171. len, ATTR_ROOT);
  172. kfree(xfs_acl);
  173. } else {
  174. /*
  175. * A NULL ACL argument means we want to remove the ACL.
  176. */
  177. error = -xfs_attr_remove(ip, ea_name, ATTR_ROOT);
  178. /*
  179. * If the attribute didn't exist to start with that's fine.
  180. */
  181. if (error == -ENOATTR)
  182. error = 0;
  183. }
  184. if (!error)
  185. set_cached_acl(inode, type, acl);
  186. return error;
  187. }
  188. int
  189. xfs_check_acl(struct inode *inode, int mask)
  190. {
  191. struct xfs_inode *ip = XFS_I(inode);
  192. struct posix_acl *acl;
  193. int error = -EAGAIN;
  194. xfs_itrace_entry(ip);
  195. /*
  196. * If there is no attribute fork no ACL exists on this inode and
  197. * we can skip the whole exercise.
  198. */
  199. if (!XFS_IFORK_Q(ip))
  200. return -EAGAIN;
  201. acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
  202. if (IS_ERR(acl))
  203. return PTR_ERR(acl);
  204. if (acl) {
  205. error = posix_acl_permission(inode, acl, mask);
  206. posix_acl_release(acl);
  207. }
  208. return error;
  209. }
  210. static int
  211. xfs_set_mode(struct inode *inode, mode_t mode)
  212. {
  213. int error = 0;
  214. if (mode != inode->i_mode) {
  215. struct iattr iattr;
  216. iattr.ia_valid = ATTR_MODE;
  217. iattr.ia_mode = mode;
  218. error = -xfs_setattr(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  219. }
  220. return error;
  221. }
  222. static int
  223. xfs_acl_exists(struct inode *inode, char *name)
  224. {
  225. int len = sizeof(struct xfs_acl);
  226. return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
  227. ATTR_ROOT|ATTR_KERNOVAL) == 0);
  228. }
  229. int
  230. posix_acl_access_exists(struct inode *inode)
  231. {
  232. return xfs_acl_exists(inode, SGI_ACL_FILE);
  233. }
  234. int
  235. posix_acl_default_exists(struct inode *inode)
  236. {
  237. if (!S_ISDIR(inode->i_mode))
  238. return 0;
  239. return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
  240. }
  241. /*
  242. * No need for i_mutex because the inode is not yet exposed to the VFS.
  243. */
  244. int
  245. xfs_inherit_acl(struct inode *inode, struct posix_acl *default_acl)
  246. {
  247. struct posix_acl *clone;
  248. mode_t mode;
  249. int error = 0, inherit = 0;
  250. if (S_ISDIR(inode->i_mode)) {
  251. error = xfs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  252. if (error)
  253. return error;
  254. }
  255. clone = posix_acl_clone(default_acl, GFP_KERNEL);
  256. if (!clone)
  257. return -ENOMEM;
  258. mode = inode->i_mode;
  259. error = posix_acl_create_masq(clone, &mode);
  260. if (error < 0)
  261. goto out_release_clone;
  262. /*
  263. * If posix_acl_create_masq returns a positive value we need to
  264. * inherit a permission that can't be represented using the Unix
  265. * mode bits and we actually need to set an ACL.
  266. */
  267. if (error > 0)
  268. inherit = 1;
  269. error = xfs_set_mode(inode, mode);
  270. if (error)
  271. goto out_release_clone;
  272. if (inherit)
  273. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  274. out_release_clone:
  275. posix_acl_release(clone);
  276. return error;
  277. }
  278. int
  279. xfs_acl_chmod(struct inode *inode)
  280. {
  281. struct posix_acl *acl, *clone;
  282. int error;
  283. if (S_ISLNK(inode->i_mode))
  284. return -EOPNOTSUPP;
  285. acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
  286. if (IS_ERR(acl) || !acl)
  287. return PTR_ERR(acl);
  288. clone = posix_acl_clone(acl, GFP_KERNEL);
  289. posix_acl_release(acl);
  290. if (!clone)
  291. return -ENOMEM;
  292. error = posix_acl_chmod_masq(clone, inode->i_mode);
  293. if (!error)
  294. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  295. posix_acl_release(clone);
  296. return error;
  297. }
  298. static int
  299. xfs_xattr_acl_get(struct dentry *dentry, const char *name,
  300. void *value, size_t size, int type)
  301. {
  302. struct posix_acl *acl;
  303. int error;
  304. acl = xfs_get_acl(dentry->d_inode, type);
  305. if (IS_ERR(acl))
  306. return PTR_ERR(acl);
  307. if (acl == NULL)
  308. return -ENODATA;
  309. error = posix_acl_to_xattr(acl, value, size);
  310. posix_acl_release(acl);
  311. return error;
  312. }
  313. static int
  314. xfs_xattr_acl_set(struct dentry *dentry, const char *name,
  315. const void *value, size_t size, int flags, int type)
  316. {
  317. struct inode *inode = dentry->d_inode;
  318. struct posix_acl *acl = NULL;
  319. int error = 0;
  320. if (flags & XATTR_CREATE)
  321. return -EINVAL;
  322. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  323. return value ? -EACCES : 0;
  324. if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
  325. return -EPERM;
  326. if (!value)
  327. goto set_acl;
  328. acl = posix_acl_from_xattr(value, size);
  329. if (!acl) {
  330. /*
  331. * acl_set_file(3) may request that we set default ACLs with
  332. * zero length -- defend (gracefully) against that here.
  333. */
  334. goto out;
  335. }
  336. if (IS_ERR(acl)) {
  337. error = PTR_ERR(acl);
  338. goto out;
  339. }
  340. error = posix_acl_valid(acl);
  341. if (error)
  342. goto out_release;
  343. error = -EINVAL;
  344. if (acl->a_count > XFS_ACL_MAX_ENTRIES)
  345. goto out_release;
  346. if (type == ACL_TYPE_ACCESS) {
  347. mode_t mode = inode->i_mode;
  348. error = posix_acl_equiv_mode(acl, &mode);
  349. if (error <= 0) {
  350. posix_acl_release(acl);
  351. acl = NULL;
  352. if (error < 0)
  353. return error;
  354. }
  355. error = xfs_set_mode(inode, mode);
  356. if (error)
  357. goto out_release;
  358. }
  359. set_acl:
  360. error = xfs_set_acl(inode, type, acl);
  361. out_release:
  362. posix_acl_release(acl);
  363. out:
  364. return error;
  365. }
  366. struct xattr_handler xfs_xattr_acl_access_handler = {
  367. .prefix = POSIX_ACL_XATTR_ACCESS,
  368. .flags = ACL_TYPE_ACCESS,
  369. .get = xfs_xattr_acl_get,
  370. .set = xfs_xattr_acl_set,
  371. };
  372. struct xattr_handler xfs_xattr_acl_default_handler = {
  373. .prefix = POSIX_ACL_XATTR_DEFAULT,
  374. .flags = ACL_TYPE_DEFAULT,
  375. .get = xfs_xattr_acl_get,
  376. .set = xfs_xattr_acl_set,
  377. };