xattr_acl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. if (value)
  268. kfree(value);
  269. if (!error) {
  270. /* Release the old one */
  271. if (!IS_ERR(*p_acl) && *p_acl)
  272. posix_acl_release(*p_acl);
  273. if (acl == NULL)
  274. *p_acl = ERR_PTR(-ENODATA);
  275. else
  276. *p_acl = posix_acl_dup(acl);
  277. }
  278. return error;
  279. }
  280. /* dir->i_sem: down,
  281. * inode is new and not released into the wild yet */
  282. int
  283. reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
  284. struct inode *inode)
  285. {
  286. struct posix_acl *acl;
  287. int err = 0;
  288. /* ACLs only get applied to files and directories */
  289. if (S_ISLNK(inode->i_mode))
  290. return 0;
  291. /* ACLs can only be used on "new" objects, so if it's an old object
  292. * there is nothing to inherit from */
  293. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  294. goto apply_umask;
  295. /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  296. * would be useless since permissions are ignored, and a pain because
  297. * it introduces locking cycles */
  298. if (is_reiserfs_priv_object(dir)) {
  299. reiserfs_mark_inode_private(inode);
  300. goto apply_umask;
  301. }
  302. acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
  303. if (IS_ERR(acl)) {
  304. if (PTR_ERR(acl) == -ENODATA)
  305. goto apply_umask;
  306. return PTR_ERR(acl);
  307. }
  308. if (acl) {
  309. struct posix_acl *acl_copy;
  310. mode_t mode = inode->i_mode;
  311. int need_acl;
  312. /* Copy the default ACL to the default ACL of a new directory */
  313. if (S_ISDIR(inode->i_mode)) {
  314. err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  315. if (err)
  316. goto cleanup;
  317. }
  318. /* Now we reconcile the new ACL and the mode,
  319. potentially modifying both */
  320. acl_copy = posix_acl_clone(acl, GFP_NOFS);
  321. if (!acl_copy) {
  322. err = -ENOMEM;
  323. goto cleanup;
  324. }
  325. need_acl = posix_acl_create_masq(acl_copy, &mode);
  326. if (need_acl >= 0) {
  327. if (mode != inode->i_mode) {
  328. inode->i_mode = mode;
  329. }
  330. /* If we need an ACL.. */
  331. if (need_acl > 0) {
  332. err =
  333. reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
  334. 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 reiserfs_cache_default_acl(struct inode *inode)
  354. {
  355. int ret = 0;
  356. if (reiserfs_posixacl(inode->i_sb) && !is_reiserfs_priv_object(inode)) {
  357. struct posix_acl *acl;
  358. reiserfs_read_lock_xattr_i(inode);
  359. reiserfs_read_lock_xattrs(inode->i_sb);
  360. acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
  361. reiserfs_read_unlock_xattrs(inode->i_sb);
  362. reiserfs_read_unlock_xattr_i(inode);
  363. ret = acl ? 1 : 0;
  364. posix_acl_release(acl);
  365. }
  366. return ret;
  367. }
  368. int reiserfs_acl_chmod(struct inode *inode)
  369. {
  370. struct posix_acl *acl, *clone;
  371. int error;
  372. if (S_ISLNK(inode->i_mode))
  373. return -EOPNOTSUPP;
  374. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  375. !reiserfs_posixacl(inode->i_sb)) {
  376. return 0;
  377. }
  378. reiserfs_read_lock_xattrs(inode->i_sb);
  379. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  380. reiserfs_read_unlock_xattrs(inode->i_sb);
  381. if (!acl)
  382. return 0;
  383. if (IS_ERR(acl))
  384. return PTR_ERR(acl);
  385. clone = posix_acl_clone(acl, GFP_NOFS);
  386. posix_acl_release(acl);
  387. if (!clone)
  388. return -ENOMEM;
  389. error = posix_acl_chmod_masq(clone, inode->i_mode);
  390. if (!error) {
  391. int lock = !has_xattr_dir(inode);
  392. reiserfs_write_lock_xattr_i(inode);
  393. if (lock)
  394. reiserfs_write_lock_xattrs(inode->i_sb);
  395. else
  396. reiserfs_read_lock_xattrs(inode->i_sb);
  397. error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  398. if (lock)
  399. reiserfs_write_unlock_xattrs(inode->i_sb);
  400. else
  401. reiserfs_read_unlock_xattrs(inode->i_sb);
  402. reiserfs_write_unlock_xattr_i(inode);
  403. }
  404. posix_acl_release(clone);
  405. return error;
  406. }
  407. static int
  408. posix_acl_access_get(struct inode *inode, const char *name,
  409. void *buffer, size_t size)
  410. {
  411. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  412. return -EINVAL;
  413. return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  414. }
  415. static int
  416. posix_acl_access_set(struct inode *inode, const char *name,
  417. const void *value, size_t size, int flags)
  418. {
  419. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  420. return -EINVAL;
  421. return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  422. }
  423. static int posix_acl_access_del(struct inode *inode, const char *name)
  424. {
  425. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  426. struct posix_acl **acl = &reiserfs_i->i_acl_access;
  427. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  428. return -EINVAL;
  429. if (!IS_ERR(*acl) && *acl) {
  430. posix_acl_release(*acl);
  431. *acl = ERR_PTR(-ENODATA);
  432. }
  433. return 0;
  434. }
  435. static int
  436. posix_acl_access_list(struct inode *inode, const char *name, int namelen,
  437. char *out)
  438. {
  439. int len = namelen;
  440. if (!reiserfs_posixacl(inode->i_sb))
  441. return 0;
  442. if (out)
  443. memcpy(out, name, len);
  444. return len;
  445. }
  446. struct reiserfs_xattr_handler posix_acl_access_handler = {
  447. .prefix = POSIX_ACL_XATTR_ACCESS,
  448. .get = posix_acl_access_get,
  449. .set = posix_acl_access_set,
  450. .del = posix_acl_access_del,
  451. .list = posix_acl_access_list,
  452. };
  453. static int
  454. posix_acl_default_get(struct inode *inode, const char *name,
  455. void *buffer, size_t size)
  456. {
  457. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  458. return -EINVAL;
  459. return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  460. }
  461. static int
  462. posix_acl_default_set(struct inode *inode, const char *name,
  463. const void *value, size_t size, int flags)
  464. {
  465. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  466. return -EINVAL;
  467. return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  468. }
  469. static int posix_acl_default_del(struct inode *inode, const char *name)
  470. {
  471. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  472. struct posix_acl **acl = &reiserfs_i->i_acl_default;
  473. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  474. return -EINVAL;
  475. if (!IS_ERR(*acl) && *acl) {
  476. posix_acl_release(*acl);
  477. *acl = ERR_PTR(-ENODATA);
  478. }
  479. return 0;
  480. }
  481. static int
  482. posix_acl_default_list(struct inode *inode, const char *name, int namelen,
  483. char *out)
  484. {
  485. int len = namelen;
  486. if (!reiserfs_posixacl(inode->i_sb))
  487. return 0;
  488. if (out)
  489. memcpy(out, name, len);
  490. return len;
  491. }
  492. struct reiserfs_xattr_handler posix_acl_default_handler = {
  493. .prefix = POSIX_ACL_XATTR_DEFAULT,
  494. .get = posix_acl_default_get,
  495. .set = posix_acl_default_set,
  496. .del = posix_acl_default_del,
  497. .list = posix_acl_default_list,
  498. };