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