xfs_acl.c 9.4 KB

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