xfs_acl.c 8.9 KB

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