xfs_acl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_large(len, KM_SLEEP);
  138. if (!xfs_acl)
  139. return ERR_PTR(-ENOMEM);
  140. error = -xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
  141. &len, ATTR_ROOT);
  142. if (error) {
  143. /*
  144. * If the attribute doesn't exist make sure we have a negative
  145. * cache entry, for any other error assume it is transient and
  146. * leave the cache entry as ACL_NOT_CACHED.
  147. */
  148. if (error == -ENOATTR) {
  149. acl = NULL;
  150. goto out_update_cache;
  151. }
  152. goto out;
  153. }
  154. acl = xfs_acl_from_disk(xfs_acl, XFS_ACL_MAX_ENTRIES(ip->i_mount));
  155. if (IS_ERR(acl))
  156. goto out;
  157. out_update_cache:
  158. set_cached_acl(inode, type, acl);
  159. out:
  160. kmem_free(xfs_acl);
  161. return acl;
  162. }
  163. STATIC int
  164. xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  165. {
  166. struct xfs_inode *ip = XFS_I(inode);
  167. unsigned char *ea_name;
  168. int error;
  169. if (S_ISLNK(inode->i_mode))
  170. return -EOPNOTSUPP;
  171. switch (type) {
  172. case ACL_TYPE_ACCESS:
  173. ea_name = SGI_ACL_FILE;
  174. break;
  175. case ACL_TYPE_DEFAULT:
  176. if (!S_ISDIR(inode->i_mode))
  177. return acl ? -EACCES : 0;
  178. ea_name = SGI_ACL_DEFAULT;
  179. break;
  180. default:
  181. return -EINVAL;
  182. }
  183. if (acl) {
  184. struct xfs_acl *xfs_acl;
  185. int len = XFS_ACL_MAX_SIZE(ip->i_mount);
  186. xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
  187. if (!xfs_acl)
  188. return -ENOMEM;
  189. xfs_acl_to_disk(xfs_acl, acl);
  190. /* subtract away the unused acl entries */
  191. len -= sizeof(struct xfs_acl_entry) *
  192. (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
  193. error = -xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
  194. len, ATTR_ROOT);
  195. kmem_free(xfs_acl);
  196. } else {
  197. /*
  198. * A NULL ACL argument means we want to remove the ACL.
  199. */
  200. error = -xfs_attr_remove(ip, ea_name, ATTR_ROOT);
  201. /*
  202. * If the attribute didn't exist to start with that's fine.
  203. */
  204. if (error == -ENOATTR)
  205. error = 0;
  206. }
  207. if (!error)
  208. set_cached_acl(inode, type, acl);
  209. return error;
  210. }
  211. static int
  212. xfs_set_mode(struct inode *inode, umode_t mode)
  213. {
  214. int error = 0;
  215. if (mode != inode->i_mode) {
  216. struct iattr iattr;
  217. iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
  218. iattr.ia_mode = mode;
  219. iattr.ia_ctime = current_fs_time(inode->i_sb);
  220. error = -xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  221. }
  222. return error;
  223. }
  224. static int
  225. xfs_acl_exists(struct inode *inode, unsigned char *name)
  226. {
  227. int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb));
  228. return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
  229. ATTR_ROOT|ATTR_KERNOVAL) == 0);
  230. }
  231. int
  232. posix_acl_access_exists(struct inode *inode)
  233. {
  234. return xfs_acl_exists(inode, SGI_ACL_FILE);
  235. }
  236. int
  237. posix_acl_default_exists(struct inode *inode)
  238. {
  239. if (!S_ISDIR(inode->i_mode))
  240. return 0;
  241. return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
  242. }
  243. /*
  244. * No need for i_mutex because the inode is not yet exposed to the VFS.
  245. */
  246. int
  247. xfs_inherit_acl(struct inode *inode, struct posix_acl *acl)
  248. {
  249. umode_t mode = inode->i_mode;
  250. int error = 0, inherit = 0;
  251. if (S_ISDIR(inode->i_mode)) {
  252. error = xfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  253. if (error)
  254. goto out;
  255. }
  256. error = posix_acl_create(&acl, GFP_KERNEL, &mode);
  257. if (error < 0)
  258. return error;
  259. /*
  260. * If posix_acl_create returns a positive value we need to
  261. * inherit a permission that can't be represented using the Unix
  262. * mode bits and we actually need to set an ACL.
  263. */
  264. if (error > 0)
  265. inherit = 1;
  266. error = xfs_set_mode(inode, mode);
  267. if (error)
  268. goto out;
  269. if (inherit)
  270. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, acl);
  271. out:
  272. posix_acl_release(acl);
  273. return error;
  274. }
  275. int
  276. xfs_acl_chmod(struct inode *inode)
  277. {
  278. struct posix_acl *acl;
  279. int error;
  280. if (S_ISLNK(inode->i_mode))
  281. return -EOPNOTSUPP;
  282. acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
  283. if (IS_ERR(acl) || !acl)
  284. return PTR_ERR(acl);
  285. error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  286. if (error)
  287. return error;
  288. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, acl);
  289. posix_acl_release(acl);
  290. return error;
  291. }
  292. static int
  293. xfs_xattr_acl_get(struct dentry *dentry, const char *name,
  294. void *value, size_t size, int type)
  295. {
  296. struct posix_acl *acl;
  297. int error;
  298. acl = xfs_get_acl(dentry->d_inode, type);
  299. if (IS_ERR(acl))
  300. return PTR_ERR(acl);
  301. if (acl == NULL)
  302. return -ENODATA;
  303. error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  304. posix_acl_release(acl);
  305. return error;
  306. }
  307. static int
  308. xfs_xattr_acl_set(struct dentry *dentry, const char *name,
  309. const void *value, size_t size, int flags, int type)
  310. {
  311. struct inode *inode = dentry->d_inode;
  312. struct posix_acl *acl = NULL;
  313. int error = 0;
  314. if (flags & XATTR_CREATE)
  315. return -EINVAL;
  316. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  317. return value ? -EACCES : 0;
  318. if (!inode_owner_or_capable(inode))
  319. return -EPERM;
  320. if (!value)
  321. goto set_acl;
  322. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  323. if (!acl) {
  324. /*
  325. * acl_set_file(3) may request that we set default ACLs with
  326. * zero length -- defend (gracefully) against that here.
  327. */
  328. goto out;
  329. }
  330. if (IS_ERR(acl)) {
  331. error = PTR_ERR(acl);
  332. goto out;
  333. }
  334. error = posix_acl_valid(acl);
  335. if (error)
  336. goto out_release;
  337. error = -EINVAL;
  338. if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
  339. goto out_release;
  340. if (type == ACL_TYPE_ACCESS) {
  341. umode_t mode = inode->i_mode;
  342. error = posix_acl_equiv_mode(acl, &mode);
  343. if (error <= 0) {
  344. posix_acl_release(acl);
  345. acl = NULL;
  346. if (error < 0)
  347. return error;
  348. }
  349. error = xfs_set_mode(inode, mode);
  350. if (error)
  351. goto out_release;
  352. }
  353. set_acl:
  354. error = xfs_set_acl(inode, type, acl);
  355. out_release:
  356. posix_acl_release(acl);
  357. out:
  358. return error;
  359. }
  360. const struct xattr_handler xfs_xattr_acl_access_handler = {
  361. .prefix = POSIX_ACL_XATTR_ACCESS,
  362. .flags = ACL_TYPE_ACCESS,
  363. .get = xfs_xattr_acl_get,
  364. .set = xfs_xattr_acl_set,
  365. };
  366. const struct xattr_handler xfs_xattr_acl_default_handler = {
  367. .prefix = POSIX_ACL_XATTR_DEFAULT,
  368. .flags = ACL_TYPE_DEFAULT,
  369. .get = xfs_xattr_acl_get,
  370. .set = xfs_xattr_acl_set,
  371. };