xattr_acl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include <linux/capability.h>
  2. #include <linux/fs.h>
  3. #include <linux/posix_acl.h>
  4. #include <linux/reiserfs_fs.h>
  5. #include <linux/errno.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/xattr.h>
  8. #include <linux/posix_acl_xattr.h>
  9. #include <linux/reiserfs_xattr.h>
  10. #include <linux/reiserfs_acl.h>
  11. #include <asm/uaccess.h>
  12. static int reiserfs_set_acl(struct inode *inode, int type,
  13. struct posix_acl *acl);
  14. static int
  15. xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
  16. {
  17. struct posix_acl *acl;
  18. int error;
  19. if (!reiserfs_posixacl(inode->i_sb))
  20. return -EOPNOTSUPP;
  21. if (!is_owner_or_cap(inode))
  22. return -EPERM;
  23. if (value) {
  24. acl = posix_acl_from_xattr(value, size);
  25. if (IS_ERR(acl)) {
  26. return PTR_ERR(acl);
  27. } else if (acl) {
  28. error = posix_acl_valid(acl);
  29. if (error)
  30. goto release_and_out;
  31. }
  32. } else
  33. acl = NULL;
  34. error = reiserfs_set_acl(inode, type, acl);
  35. release_and_out:
  36. posix_acl_release(acl);
  37. return error;
  38. }
  39. static int
  40. xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
  41. {
  42. struct posix_acl *acl;
  43. int error;
  44. if (!reiserfs_posixacl(inode->i_sb))
  45. return -EOPNOTSUPP;
  46. acl = reiserfs_get_acl(inode, type);
  47. if (IS_ERR(acl))
  48. return PTR_ERR(acl);
  49. if (acl == NULL)
  50. return -ENODATA;
  51. error = posix_acl_to_xattr(acl, buffer, size);
  52. posix_acl_release(acl);
  53. return error;
  54. }
  55. /*
  56. * Convert from filesystem to in-memory representation.
  57. */
  58. static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
  59. {
  60. const char *end = (char *)value + size;
  61. int n, count;
  62. struct posix_acl *acl;
  63. if (!value)
  64. return NULL;
  65. if (size < sizeof(reiserfs_acl_header))
  66. return ERR_PTR(-EINVAL);
  67. if (((reiserfs_acl_header *) value)->a_version !=
  68. cpu_to_le32(REISERFS_ACL_VERSION))
  69. return ERR_PTR(-EINVAL);
  70. value = (char *)value + sizeof(reiserfs_acl_header);
  71. count = reiserfs_acl_count(size);
  72. if (count < 0)
  73. return ERR_PTR(-EINVAL);
  74. if (count == 0)
  75. return NULL;
  76. acl = posix_acl_alloc(count, GFP_NOFS);
  77. if (!acl)
  78. return ERR_PTR(-ENOMEM);
  79. for (n = 0; n < count; n++) {
  80. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
  81. if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
  82. goto fail;
  83. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  84. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  85. switch (acl->a_entries[n].e_tag) {
  86. case ACL_USER_OBJ:
  87. case ACL_GROUP_OBJ:
  88. case ACL_MASK:
  89. case ACL_OTHER:
  90. value = (char *)value +
  91. sizeof(reiserfs_acl_entry_short);
  92. acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
  93. break;
  94. case ACL_USER:
  95. case ACL_GROUP:
  96. value = (char *)value + sizeof(reiserfs_acl_entry);
  97. if ((char *)value > end)
  98. goto fail;
  99. acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
  100. break;
  101. default:
  102. goto fail;
  103. }
  104. }
  105. if (value != end)
  106. goto fail;
  107. return acl;
  108. fail:
  109. posix_acl_release(acl);
  110. return ERR_PTR(-EINVAL);
  111. }
  112. /*
  113. * Convert from in-memory to filesystem representation.
  114. */
  115. static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
  116. {
  117. reiserfs_acl_header *ext_acl;
  118. char *e;
  119. int n;
  120. *size = reiserfs_acl_size(acl->a_count);
  121. ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
  122. acl->a_count *
  123. sizeof(reiserfs_acl_entry),
  124. GFP_NOFS);
  125. if (!ext_acl)
  126. return ERR_PTR(-ENOMEM);
  127. ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
  128. e = (char *)ext_acl + sizeof(reiserfs_acl_header);
  129. for (n = 0; n < acl->a_count; n++) {
  130. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
  131. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  132. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  133. switch (acl->a_entries[n].e_tag) {
  134. case ACL_USER:
  135. case ACL_GROUP:
  136. entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
  137. e += sizeof(reiserfs_acl_entry);
  138. break;
  139. case ACL_USER_OBJ:
  140. case ACL_GROUP_OBJ:
  141. case ACL_MASK:
  142. case ACL_OTHER:
  143. e += sizeof(reiserfs_acl_entry_short);
  144. break;
  145. default:
  146. goto fail;
  147. }
  148. }
  149. return (char *)ext_acl;
  150. fail:
  151. kfree(ext_acl);
  152. return ERR_PTR(-EINVAL);
  153. }
  154. static inline void iset_acl(struct inode *inode, struct posix_acl **i_acl,
  155. struct posix_acl *acl)
  156. {
  157. spin_lock(&inode->i_lock);
  158. if (*i_acl != ERR_PTR(-ENODATA))
  159. posix_acl_release(*i_acl);
  160. *i_acl = posix_acl_dup(acl);
  161. spin_unlock(&inode->i_lock);
  162. }
  163. static inline struct posix_acl *iget_acl(struct inode *inode,
  164. struct posix_acl **i_acl)
  165. {
  166. struct posix_acl *acl = ERR_PTR(-ENODATA);
  167. spin_lock(&inode->i_lock);
  168. if (*i_acl != ERR_PTR(-ENODATA))
  169. acl = posix_acl_dup(*i_acl);
  170. spin_unlock(&inode->i_lock);
  171. return acl;
  172. }
  173. /*
  174. * Inode operation get_posix_acl().
  175. *
  176. * inode->i_mutex: down
  177. * BKL held [before 2.5.x]
  178. */
  179. struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
  180. {
  181. char *name, *value;
  182. struct posix_acl *acl, **p_acl;
  183. int size;
  184. int retval;
  185. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  186. switch (type) {
  187. case ACL_TYPE_ACCESS:
  188. name = POSIX_ACL_XATTR_ACCESS;
  189. p_acl = &reiserfs_i->i_acl_access;
  190. break;
  191. case ACL_TYPE_DEFAULT:
  192. name = POSIX_ACL_XATTR_DEFAULT;
  193. p_acl = &reiserfs_i->i_acl_default;
  194. break;
  195. default:
  196. return ERR_PTR(-EINVAL);
  197. }
  198. acl = iget_acl(inode, p_acl);
  199. if (acl && !IS_ERR(acl))
  200. return acl;
  201. else if (PTR_ERR(acl) == -ENODATA)
  202. return NULL;
  203. size = reiserfs_xattr_get(inode, name, NULL, 0);
  204. if (size < 0) {
  205. if (size == -ENODATA || size == -ENOSYS) {
  206. *p_acl = ERR_PTR(-ENODATA);
  207. return NULL;
  208. }
  209. return ERR_PTR(size);
  210. }
  211. value = kmalloc(size, GFP_NOFS);
  212. if (!value)
  213. return ERR_PTR(-ENOMEM);
  214. retval = reiserfs_xattr_get(inode, name, value, size);
  215. if (retval == -ENODATA || retval == -ENOSYS) {
  216. /* This shouldn't actually happen as it should have
  217. been caught above.. but just in case */
  218. acl = NULL;
  219. *p_acl = ERR_PTR(-ENODATA);
  220. } else if (retval < 0) {
  221. acl = ERR_PTR(retval);
  222. } else {
  223. acl = posix_acl_from_disk(value, retval);
  224. if (!IS_ERR(acl))
  225. iset_acl(inode, p_acl, acl);
  226. }
  227. kfree(value);
  228. return acl;
  229. }
  230. /*
  231. * Inode operation set_posix_acl().
  232. *
  233. * inode->i_mutex: down
  234. * BKL held [before 2.5.x]
  235. */
  236. static int
  237. reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  238. {
  239. char *name;
  240. void *value = NULL;
  241. struct posix_acl **p_acl;
  242. size_t size;
  243. int error;
  244. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  245. if (S_ISLNK(inode->i_mode))
  246. return -EOPNOTSUPP;
  247. switch (type) {
  248. case ACL_TYPE_ACCESS:
  249. name = POSIX_ACL_XATTR_ACCESS;
  250. p_acl = &reiserfs_i->i_acl_access;
  251. if (acl) {
  252. mode_t mode = inode->i_mode;
  253. error = posix_acl_equiv_mode(acl, &mode);
  254. if (error < 0)
  255. return error;
  256. else {
  257. inode->i_mode = mode;
  258. if (error == 0)
  259. acl = NULL;
  260. }
  261. }
  262. break;
  263. case ACL_TYPE_DEFAULT:
  264. name = POSIX_ACL_XATTR_DEFAULT;
  265. p_acl = &reiserfs_i->i_acl_default;
  266. if (!S_ISDIR(inode->i_mode))
  267. return acl ? -EACCES : 0;
  268. break;
  269. default:
  270. return -EINVAL;
  271. }
  272. if (acl) {
  273. value = posix_acl_to_disk(acl, &size);
  274. if (IS_ERR(value))
  275. return (int)PTR_ERR(value);
  276. error = reiserfs_xattr_set(inode, name, value, size, 0);
  277. } else {
  278. error = reiserfs_xattr_del(inode, name);
  279. if (error == -ENODATA) {
  280. /* This may seem odd here, but it means that the ACL was set
  281. * with a value representable with mode bits. If there was
  282. * an ACL before, reiserfs_xattr_del already dirtied the inode.
  283. */
  284. mark_inode_dirty(inode);
  285. error = 0;
  286. }
  287. }
  288. kfree(value);
  289. if (!error)
  290. iset_acl(inode, p_acl, acl);
  291. return error;
  292. }
  293. /* dir->i_mutex: locked,
  294. * inode is new and not released into the wild yet */
  295. int
  296. reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
  297. struct inode *inode)
  298. {
  299. struct posix_acl *acl;
  300. int err = 0;
  301. /* ACLs only get applied to files and directories */
  302. if (S_ISLNK(inode->i_mode))
  303. return 0;
  304. /* ACLs can only be used on "new" objects, so if it's an old object
  305. * there is nothing to inherit from */
  306. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  307. goto apply_umask;
  308. /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  309. * would be useless since permissions are ignored, and a pain because
  310. * it introduces locking cycles */
  311. if (IS_PRIVATE(dir)) {
  312. inode->i_flags |= S_PRIVATE;
  313. goto apply_umask;
  314. }
  315. acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
  316. if (IS_ERR(acl)) {
  317. if (PTR_ERR(acl) == -ENODATA)
  318. goto apply_umask;
  319. return PTR_ERR(acl);
  320. }
  321. if (acl) {
  322. struct posix_acl *acl_copy;
  323. mode_t mode = inode->i_mode;
  324. int need_acl;
  325. /* Copy the default ACL to the default ACL of a new directory */
  326. if (S_ISDIR(inode->i_mode)) {
  327. err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  328. if (err)
  329. goto cleanup;
  330. }
  331. /* Now we reconcile the new ACL and the mode,
  332. potentially modifying both */
  333. acl_copy = posix_acl_clone(acl, GFP_NOFS);
  334. if (!acl_copy) {
  335. err = -ENOMEM;
  336. goto cleanup;
  337. }
  338. need_acl = posix_acl_create_masq(acl_copy, &mode);
  339. if (need_acl >= 0) {
  340. if (mode != inode->i_mode) {
  341. inode->i_mode = mode;
  342. }
  343. /* If we need an ACL.. */
  344. if (need_acl > 0) {
  345. err =
  346. reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
  347. acl_copy);
  348. if (err)
  349. goto cleanup_copy;
  350. }
  351. }
  352. cleanup_copy:
  353. posix_acl_release(acl_copy);
  354. cleanup:
  355. posix_acl_release(acl);
  356. } else {
  357. apply_umask:
  358. /* no ACL, apply umask */
  359. inode->i_mode &= ~current->fs->umask;
  360. }
  361. return err;
  362. }
  363. /* Looks up and caches the result of the default ACL.
  364. * We do this so that we don't need to carry the xattr_sem into
  365. * reiserfs_new_inode if we don't need to */
  366. int reiserfs_cache_default_acl(struct inode *inode)
  367. {
  368. int ret = 0;
  369. if (reiserfs_posixacl(inode->i_sb) && !IS_PRIVATE(inode)) {
  370. struct posix_acl *acl;
  371. reiserfs_read_lock_xattr_i(inode);
  372. acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
  373. reiserfs_read_unlock_xattr_i(inode);
  374. ret = (acl && !IS_ERR(acl));
  375. if (ret)
  376. posix_acl_release(acl);
  377. }
  378. return ret;
  379. }
  380. int reiserfs_acl_chmod(struct inode *inode)
  381. {
  382. struct posix_acl *acl, *clone;
  383. int error;
  384. if (S_ISLNK(inode->i_mode))
  385. return -EOPNOTSUPP;
  386. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  387. !reiserfs_posixacl(inode->i_sb)) {
  388. return 0;
  389. }
  390. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  391. if (!acl)
  392. return 0;
  393. if (IS_ERR(acl))
  394. return PTR_ERR(acl);
  395. clone = posix_acl_clone(acl, GFP_NOFS);
  396. posix_acl_release(acl);
  397. if (!clone)
  398. return -ENOMEM;
  399. error = posix_acl_chmod_masq(clone, inode->i_mode);
  400. if (!error) {
  401. reiserfs_write_lock_xattr_i(inode);
  402. error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  403. reiserfs_write_unlock_xattr_i(inode);
  404. }
  405. posix_acl_release(clone);
  406. return error;
  407. }
  408. static int
  409. posix_acl_access_get(struct inode *inode, const char *name,
  410. void *buffer, size_t size)
  411. {
  412. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  413. return -EINVAL;
  414. return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  415. }
  416. static int
  417. posix_acl_access_set(struct inode *inode, const char *name,
  418. const void *value, size_t size, int flags)
  419. {
  420. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  421. return -EINVAL;
  422. return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  423. }
  424. static int posix_acl_access_del(struct inode *inode, const char *name)
  425. {
  426. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  427. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  428. return -EINVAL;
  429. iset_acl(inode, &reiserfs_i->i_acl_access, ERR_PTR(-ENODATA));
  430. return 0;
  431. }
  432. static int
  433. posix_acl_access_list(struct inode *inode, const char *name, int namelen,
  434. char *out)
  435. {
  436. int len = namelen;
  437. if (!reiserfs_posixacl(inode->i_sb))
  438. return 0;
  439. if (out)
  440. memcpy(out, name, len);
  441. return len;
  442. }
  443. struct reiserfs_xattr_handler posix_acl_access_handler = {
  444. .prefix = POSIX_ACL_XATTR_ACCESS,
  445. .get = posix_acl_access_get,
  446. .set = posix_acl_access_set,
  447. .del = posix_acl_access_del,
  448. .list = posix_acl_access_list,
  449. };
  450. static int
  451. posix_acl_default_get(struct inode *inode, const char *name,
  452. void *buffer, size_t size)
  453. {
  454. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  455. return -EINVAL;
  456. return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  457. }
  458. static int
  459. posix_acl_default_set(struct inode *inode, const char *name,
  460. const void *value, size_t size, int flags)
  461. {
  462. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  463. return -EINVAL;
  464. return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  465. }
  466. static int posix_acl_default_del(struct inode *inode, const char *name)
  467. {
  468. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  469. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  470. return -EINVAL;
  471. iset_acl(inode, &reiserfs_i->i_acl_default, ERR_PTR(-ENODATA));
  472. return 0;
  473. }
  474. static int
  475. posix_acl_default_list(struct inode *inode, const char *name, int namelen,
  476. char *out)
  477. {
  478. int len = namelen;
  479. if (!reiserfs_posixacl(inode->i_sb))
  480. return 0;
  481. if (out)
  482. memcpy(out, name, len);
  483. return len;
  484. }
  485. struct reiserfs_xattr_handler posix_acl_default_handler = {
  486. .prefix = POSIX_ACL_XATTR_DEFAULT,
  487. .get = posix_acl_default_get,
  488. .set = posix_acl_default_set,
  489. .del = posix_acl_default_del,
  490. .list = posix_acl_default_list,
  491. };