xattr_acl.c 15 KB

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