xfs_acl.c 8.8 KB

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