xfs_acl.c 8.9 KB

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