xfs_acl.c 8.9 KB

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