xfs_acl.c 9.5 KB

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