xattr_acl.c 13 KB

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