xfs_acl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 <linux/xattr.h>
  25. #include <linux/posix_acl_xattr.h>
  26. #define XFS_ACL_NOT_CACHED ((void *)-1)
  27. /*
  28. * Locking scheme:
  29. * - all ACL updates are protected by inode->i_mutex, which is taken before
  30. * calling into this file.
  31. * - access and updates to the ip->i_acl and ip->i_default_acl pointers are
  32. * protected by inode->i_lock.
  33. */
  34. STATIC struct posix_acl *
  35. xfs_acl_from_disk(struct xfs_acl *aclp)
  36. {
  37. struct posix_acl_entry *acl_e;
  38. struct posix_acl *acl;
  39. struct xfs_acl_entry *ace;
  40. int count, i;
  41. count = be32_to_cpu(aclp->acl_cnt);
  42. acl = posix_acl_alloc(count, GFP_KERNEL);
  43. if (!acl)
  44. return ERR_PTR(-ENOMEM);
  45. for (i = 0; i < count; i++) {
  46. acl_e = &acl->a_entries[i];
  47. ace = &aclp->acl_entry[i];
  48. /*
  49. * The tag is 32 bits on disk and 16 bits in core.
  50. *
  51. * Because every access to it goes through the core
  52. * format first this is not a problem.
  53. */
  54. acl_e->e_tag = be32_to_cpu(ace->ae_tag);
  55. acl_e->e_perm = be16_to_cpu(ace->ae_perm);
  56. switch (acl_e->e_tag) {
  57. case ACL_USER:
  58. case ACL_GROUP:
  59. acl_e->e_id = be32_to_cpu(ace->ae_id);
  60. break;
  61. case ACL_USER_OBJ:
  62. case ACL_GROUP_OBJ:
  63. case ACL_MASK:
  64. case ACL_OTHER:
  65. acl_e->e_id = ACL_UNDEFINED_ID;
  66. break;
  67. default:
  68. goto fail;
  69. }
  70. }
  71. return acl;
  72. fail:
  73. posix_acl_release(acl);
  74. return ERR_PTR(-EINVAL);
  75. }
  76. STATIC void
  77. xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
  78. {
  79. const struct posix_acl_entry *acl_e;
  80. struct xfs_acl_entry *ace;
  81. int i;
  82. aclp->acl_cnt = cpu_to_be32(acl->a_count);
  83. for (i = 0; i < acl->a_count; i++) {
  84. ace = &aclp->acl_entry[i];
  85. acl_e = &acl->a_entries[i];
  86. ace->ae_tag = cpu_to_be32(acl_e->e_tag);
  87. ace->ae_id = cpu_to_be32(acl_e->e_id);
  88. ace->ae_perm = cpu_to_be16(acl_e->e_perm);
  89. }
  90. }
  91. /*
  92. * Update the cached ACL pointer in the inode.
  93. *
  94. * Because we don't hold any locks while reading/writing the attribute
  95. * from/to disk another thread could have raced and updated the cached
  96. * ACL value before us. In that case we release the previous cached value
  97. * and update it with our new value.
  98. */
  99. STATIC void
  100. xfs_update_cached_acl(struct inode *inode, struct posix_acl **p_acl,
  101. struct posix_acl *acl)
  102. {
  103. spin_lock(&inode->i_lock);
  104. if (*p_acl && *p_acl != XFS_ACL_NOT_CACHED)
  105. posix_acl_release(*p_acl);
  106. *p_acl = posix_acl_dup(acl);
  107. spin_unlock(&inode->i_lock);
  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 = NULL, **p_acl;
  114. struct xfs_acl *xfs_acl;
  115. int len = sizeof(struct xfs_acl);
  116. char *ea_name;
  117. int error;
  118. switch (type) {
  119. case ACL_TYPE_ACCESS:
  120. ea_name = SGI_ACL_FILE;
  121. p_acl = &ip->i_acl;
  122. break;
  123. case ACL_TYPE_DEFAULT:
  124. ea_name = SGI_ACL_DEFAULT;
  125. p_acl = &ip->i_default_acl;
  126. break;
  127. default:
  128. return ERR_PTR(-EINVAL);
  129. }
  130. spin_lock(&inode->i_lock);
  131. if (*p_acl != XFS_ACL_NOT_CACHED)
  132. acl = posix_acl_dup(*p_acl);
  133. spin_unlock(&inode->i_lock);
  134. /*
  135. * If we have a cached ACLs value just return it, not need to
  136. * go out to the disk.
  137. */
  138. if (acl)
  139. return acl;
  140. xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
  141. if (!xfs_acl)
  142. return ERR_PTR(-ENOMEM);
  143. error = -xfs_attr_get(ip, ea_name, (char *)xfs_acl, &len, ATTR_ROOT);
  144. if (error) {
  145. /*
  146. * If the attribute doesn't exist make sure we have a negative
  147. * cache entry, for any other error assume it is transient and
  148. * leave the cache entry as XFS_ACL_NOT_CACHED.
  149. */
  150. if (error == -ENOATTR) {
  151. acl = NULL;
  152. goto out_update_cache;
  153. }
  154. goto out;
  155. }
  156. acl = xfs_acl_from_disk(xfs_acl);
  157. if (IS_ERR(acl))
  158. goto out;
  159. out_update_cache:
  160. xfs_update_cached_acl(inode, p_acl, acl);
  161. out:
  162. kfree(xfs_acl);
  163. return acl;
  164. }
  165. STATIC int
  166. xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  167. {
  168. struct xfs_inode *ip = XFS_I(inode);
  169. struct posix_acl **p_acl;
  170. char *ea_name;
  171. int error;
  172. if (S_ISLNK(inode->i_mode))
  173. return -EOPNOTSUPP;
  174. switch (type) {
  175. case ACL_TYPE_ACCESS:
  176. ea_name = SGI_ACL_FILE;
  177. p_acl = &ip->i_acl;
  178. break;
  179. case ACL_TYPE_DEFAULT:
  180. if (!S_ISDIR(inode->i_mode))
  181. return acl ? -EACCES : 0;
  182. ea_name = SGI_ACL_DEFAULT;
  183. p_acl = &ip->i_default_acl;
  184. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. if (acl) {
  189. struct xfs_acl *xfs_acl;
  190. int len;
  191. xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
  192. if (!xfs_acl)
  193. return -ENOMEM;
  194. xfs_acl_to_disk(xfs_acl, acl);
  195. len = sizeof(struct xfs_acl) -
  196. (sizeof(struct xfs_acl_entry) *
  197. (XFS_ACL_MAX_ENTRIES - acl->a_count));
  198. error = -xfs_attr_set(ip, ea_name, (char *)xfs_acl,
  199. len, ATTR_ROOT);
  200. kfree(xfs_acl);
  201. } else {
  202. /*
  203. * A NULL ACL argument means we want to remove the ACL.
  204. */
  205. error = -xfs_attr_remove(ip, ea_name, ATTR_ROOT);
  206. /*
  207. * If the attribute didn't exist to start with that's fine.
  208. */
  209. if (error == -ENOATTR)
  210. error = 0;
  211. }
  212. if (!error)
  213. xfs_update_cached_acl(inode, p_acl, acl);
  214. return error;
  215. }
  216. int
  217. xfs_check_acl(struct inode *inode, int mask)
  218. {
  219. struct xfs_inode *ip = XFS_I(inode);
  220. struct posix_acl *acl;
  221. int error = -EAGAIN;
  222. xfs_itrace_entry(ip);
  223. /*
  224. * If there is no attribute fork no ACL exists on this inode and
  225. * we can skip the whole exercise.
  226. */
  227. if (!XFS_IFORK_Q(ip))
  228. return -EAGAIN;
  229. acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
  230. if (IS_ERR(acl))
  231. return PTR_ERR(acl);
  232. if (acl) {
  233. error = posix_acl_permission(inode, acl, mask);
  234. posix_acl_release(acl);
  235. }
  236. return error;
  237. }
  238. static int
  239. xfs_set_mode(struct inode *inode, mode_t mode)
  240. {
  241. int error = 0;
  242. if (mode != inode->i_mode) {
  243. struct iattr iattr;
  244. iattr.ia_valid = ATTR_MODE;
  245. iattr.ia_mode = mode;
  246. error = -xfs_setattr(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  247. }
  248. return error;
  249. }
  250. static int
  251. xfs_acl_exists(struct inode *inode, char *name)
  252. {
  253. int len = sizeof(struct xfs_acl);
  254. return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
  255. ATTR_ROOT|ATTR_KERNOVAL) == 0);
  256. }
  257. int
  258. posix_acl_access_exists(struct inode *inode)
  259. {
  260. return xfs_acl_exists(inode, SGI_ACL_FILE);
  261. }
  262. int
  263. posix_acl_default_exists(struct inode *inode)
  264. {
  265. if (!S_ISDIR(inode->i_mode))
  266. return 0;
  267. return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
  268. }
  269. /*
  270. * No need for i_mutex because the inode is not yet exposed to the VFS.
  271. */
  272. int
  273. xfs_inherit_acl(struct inode *inode, struct posix_acl *default_acl)
  274. {
  275. struct posix_acl *clone;
  276. mode_t mode;
  277. int error = 0, inherit = 0;
  278. if (S_ISDIR(inode->i_mode)) {
  279. error = xfs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  280. if (error)
  281. return error;
  282. }
  283. clone = posix_acl_clone(default_acl, GFP_KERNEL);
  284. if (!clone)
  285. return -ENOMEM;
  286. mode = inode->i_mode;
  287. error = posix_acl_create_masq(clone, &mode);
  288. if (error < 0)
  289. goto out_release_clone;
  290. /*
  291. * If posix_acl_create_masq returns a positive value we need to
  292. * inherit a permission that can't be represented using the Unix
  293. * mode bits and we actually need to set an ACL.
  294. */
  295. if (error > 0)
  296. inherit = 1;
  297. error = xfs_set_mode(inode, mode);
  298. if (error)
  299. goto out_release_clone;
  300. if (inherit)
  301. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  302. out_release_clone:
  303. posix_acl_release(clone);
  304. return error;
  305. }
  306. int
  307. xfs_acl_chmod(struct inode *inode)
  308. {
  309. struct posix_acl *acl, *clone;
  310. int error;
  311. if (S_ISLNK(inode->i_mode))
  312. return -EOPNOTSUPP;
  313. acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
  314. if (IS_ERR(acl) || !acl)
  315. return PTR_ERR(acl);
  316. clone = posix_acl_clone(acl, GFP_KERNEL);
  317. posix_acl_release(acl);
  318. if (!clone)
  319. return -ENOMEM;
  320. error = posix_acl_chmod_masq(clone, inode->i_mode);
  321. if (!error)
  322. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  323. posix_acl_release(clone);
  324. return error;
  325. }
  326. void
  327. xfs_inode_init_acls(struct xfs_inode *ip)
  328. {
  329. /*
  330. * No need for locking, inode is not live yet.
  331. */
  332. ip->i_acl = XFS_ACL_NOT_CACHED;
  333. ip->i_default_acl = XFS_ACL_NOT_CACHED;
  334. }
  335. void
  336. xfs_inode_clear_acls(struct xfs_inode *ip)
  337. {
  338. /*
  339. * No need for locking here, the inode is not live anymore
  340. * and just about to be freed.
  341. */
  342. if (ip->i_acl != XFS_ACL_NOT_CACHED)
  343. posix_acl_release(ip->i_acl);
  344. if (ip->i_default_acl != XFS_ACL_NOT_CACHED)
  345. posix_acl_release(ip->i_default_acl);
  346. }
  347. /*
  348. * System xattr handlers.
  349. *
  350. * Currently Posix ACLs are the only system namespace extended attribute
  351. * handlers supported by XFS, so we just implement the handlers here.
  352. * If we ever support other system extended attributes this will need
  353. * some refactoring.
  354. */
  355. static int
  356. xfs_decode_acl(const char *name)
  357. {
  358. if (strcmp(name, "posix_acl_access") == 0)
  359. return ACL_TYPE_ACCESS;
  360. else if (strcmp(name, "posix_acl_default") == 0)
  361. return ACL_TYPE_DEFAULT;
  362. return -EINVAL;
  363. }
  364. static int
  365. xfs_xattr_system_get(struct inode *inode, const char *name,
  366. void *value, size_t size)
  367. {
  368. struct posix_acl *acl;
  369. int type, error;
  370. type = xfs_decode_acl(name);
  371. if (type < 0)
  372. return type;
  373. acl = xfs_get_acl(inode, type);
  374. if (IS_ERR(acl))
  375. return PTR_ERR(acl);
  376. if (acl == NULL)
  377. return -ENODATA;
  378. error = posix_acl_to_xattr(acl, value, size);
  379. posix_acl_release(acl);
  380. return error;
  381. }
  382. static int
  383. xfs_xattr_system_set(struct inode *inode, const char *name,
  384. const void *value, size_t size, int flags)
  385. {
  386. struct posix_acl *acl = NULL;
  387. int error = 0, type;
  388. type = xfs_decode_acl(name);
  389. if (type < 0)
  390. return type;
  391. if (flags & XATTR_CREATE)
  392. return -EINVAL;
  393. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  394. return value ? -EACCES : 0;
  395. if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
  396. return -EPERM;
  397. if (!value)
  398. goto set_acl;
  399. acl = posix_acl_from_xattr(value, size);
  400. if (!acl) {
  401. /*
  402. * acl_set_file(3) may request that we set default ACLs with
  403. * zero length -- defend (gracefully) against that here.
  404. */
  405. goto out;
  406. }
  407. if (IS_ERR(acl)) {
  408. error = PTR_ERR(acl);
  409. goto out;
  410. }
  411. error = posix_acl_valid(acl);
  412. if (error)
  413. goto out_release;
  414. error = -EINVAL;
  415. if (acl->a_count > XFS_ACL_MAX_ENTRIES)
  416. goto out_release;
  417. if (type == ACL_TYPE_ACCESS) {
  418. mode_t mode = inode->i_mode;
  419. error = posix_acl_equiv_mode(acl, &mode);
  420. if (error <= 0) {
  421. posix_acl_release(acl);
  422. acl = NULL;
  423. if (error < 0)
  424. return error;
  425. }
  426. error = xfs_set_mode(inode, mode);
  427. if (error)
  428. goto out_release;
  429. }
  430. set_acl:
  431. error = xfs_set_acl(inode, type, acl);
  432. out_release:
  433. posix_acl_release(acl);
  434. out:
  435. return error;
  436. }
  437. struct xattr_handler xfs_xattr_system_handler = {
  438. .prefix = XATTR_SYSTEM_PREFIX,
  439. .get = xfs_xattr_system_get,
  440. .set = xfs_xattr_system_set,
  441. };