xfs_acl.c 9.3 KB

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