xfs_acl.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 | ATTR_CTIME;
  217. iattr.ia_mode = mode;
  218. iattr.ia_ctime = current_fs_time(inode->i_sb);
  219. error = -xfs_setattr(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  220. }
  221. return error;
  222. }
  223. static int
  224. xfs_acl_exists(struct inode *inode, char *name)
  225. {
  226. int len = sizeof(struct xfs_acl);
  227. return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
  228. ATTR_ROOT|ATTR_KERNOVAL) == 0);
  229. }
  230. int
  231. posix_acl_access_exists(struct inode *inode)
  232. {
  233. return xfs_acl_exists(inode, SGI_ACL_FILE);
  234. }
  235. int
  236. posix_acl_default_exists(struct inode *inode)
  237. {
  238. if (!S_ISDIR(inode->i_mode))
  239. return 0;
  240. return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
  241. }
  242. /*
  243. * No need for i_mutex because the inode is not yet exposed to the VFS.
  244. */
  245. int
  246. xfs_inherit_acl(struct inode *inode, struct posix_acl *default_acl)
  247. {
  248. struct posix_acl *clone;
  249. mode_t mode;
  250. int error = 0, inherit = 0;
  251. if (S_ISDIR(inode->i_mode)) {
  252. error = xfs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  253. if (error)
  254. return error;
  255. }
  256. clone = posix_acl_clone(default_acl, GFP_KERNEL);
  257. if (!clone)
  258. return -ENOMEM;
  259. mode = inode->i_mode;
  260. error = posix_acl_create_masq(clone, &mode);
  261. if (error < 0)
  262. goto out_release_clone;
  263. /*
  264. * If posix_acl_create_masq returns a positive value we need to
  265. * inherit a permission that can't be represented using the Unix
  266. * mode bits and we actually need to set an ACL.
  267. */
  268. if (error > 0)
  269. inherit = 1;
  270. error = xfs_set_mode(inode, mode);
  271. if (error)
  272. goto out_release_clone;
  273. if (inherit)
  274. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  275. out_release_clone:
  276. posix_acl_release(clone);
  277. return error;
  278. }
  279. int
  280. xfs_acl_chmod(struct inode *inode)
  281. {
  282. struct posix_acl *acl, *clone;
  283. int error;
  284. if (S_ISLNK(inode->i_mode))
  285. return -EOPNOTSUPP;
  286. acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
  287. if (IS_ERR(acl) || !acl)
  288. return PTR_ERR(acl);
  289. clone = posix_acl_clone(acl, GFP_KERNEL);
  290. posix_acl_release(acl);
  291. if (!clone)
  292. return -ENOMEM;
  293. error = posix_acl_chmod_masq(clone, inode->i_mode);
  294. if (!error)
  295. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  296. posix_acl_release(clone);
  297. return error;
  298. }
  299. static int
  300. xfs_xattr_acl_get(struct dentry *dentry, const char *name,
  301. void *value, size_t size, int type)
  302. {
  303. struct posix_acl *acl;
  304. int error;
  305. acl = xfs_get_acl(dentry->d_inode, type);
  306. if (IS_ERR(acl))
  307. return PTR_ERR(acl);
  308. if (acl == NULL)
  309. return -ENODATA;
  310. error = posix_acl_to_xattr(acl, value, size);
  311. posix_acl_release(acl);
  312. return error;
  313. }
  314. static int
  315. xfs_xattr_acl_set(struct dentry *dentry, const char *name,
  316. const void *value, size_t size, int flags, int type)
  317. {
  318. struct inode *inode = dentry->d_inode;
  319. struct posix_acl *acl = NULL;
  320. int error = 0;
  321. if (flags & XATTR_CREATE)
  322. return -EINVAL;
  323. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  324. return value ? -EACCES : 0;
  325. if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
  326. return -EPERM;
  327. if (!value)
  328. goto set_acl;
  329. acl = posix_acl_from_xattr(value, size);
  330. if (!acl) {
  331. /*
  332. * acl_set_file(3) may request that we set default ACLs with
  333. * zero length -- defend (gracefully) against that here.
  334. */
  335. goto out;
  336. }
  337. if (IS_ERR(acl)) {
  338. error = PTR_ERR(acl);
  339. goto out;
  340. }
  341. error = posix_acl_valid(acl);
  342. if (error)
  343. goto out_release;
  344. error = -EINVAL;
  345. if (acl->a_count > XFS_ACL_MAX_ENTRIES)
  346. goto out_release;
  347. if (type == ACL_TYPE_ACCESS) {
  348. mode_t mode = inode->i_mode;
  349. error = posix_acl_equiv_mode(acl, &mode);
  350. if (error <= 0) {
  351. posix_acl_release(acl);
  352. acl = NULL;
  353. if (error < 0)
  354. return error;
  355. }
  356. error = xfs_set_mode(inode, mode);
  357. if (error)
  358. goto out_release;
  359. }
  360. set_acl:
  361. error = xfs_set_acl(inode, type, acl);
  362. out_release:
  363. posix_acl_release(acl);
  364. out:
  365. return error;
  366. }
  367. struct xattr_handler xfs_xattr_acl_access_handler = {
  368. .prefix = POSIX_ACL_XATTR_ACCESS,
  369. .flags = ACL_TYPE_ACCESS,
  370. .get = xfs_xattr_acl_get,
  371. .set = xfs_xattr_acl_set,
  372. };
  373. struct xattr_handler xfs_xattr_acl_default_handler = {
  374. .prefix = POSIX_ACL_XATTR_DEFAULT,
  375. .flags = ACL_TYPE_DEFAULT,
  376. .get = xfs_xattr_acl_get,
  377. .set = xfs_xattr_acl_set,
  378. };