xattr_acl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 = 0;
  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. }
  277. error = __reiserfs_xattr_set(inode, name, value, size, 0);
  278. /*
  279. * Ensure that the inode gets dirtied if we're only using
  280. * the mode bits and an old ACL didn't exist. We don't need
  281. * to check if the inode is hashed here since we won't get
  282. * called by reiserfs_inherit_default_acl().
  283. */
  284. if (error == -ENODATA) {
  285. error = 0;
  286. if (type == ACL_TYPE_ACCESS) {
  287. inode->i_ctime = CURRENT_TIME_SEC;
  288. mark_inode_dirty(inode);
  289. }
  290. }
  291. kfree(value);
  292. if (!error)
  293. iset_acl(inode, p_acl, acl);
  294. return error;
  295. }
  296. /* dir->i_mutex: locked,
  297. * inode is new and not released into the wild yet */
  298. int
  299. reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
  300. struct inode *inode)
  301. {
  302. struct posix_acl *acl;
  303. int err = 0;
  304. /* ACLs only get applied to files and directories */
  305. if (S_ISLNK(inode->i_mode))
  306. return 0;
  307. /* ACLs can only be used on "new" objects, so if it's an old object
  308. * there is nothing to inherit from */
  309. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  310. goto apply_umask;
  311. /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  312. * would be useless since permissions are ignored, and a pain because
  313. * it introduces locking cycles */
  314. if (IS_PRIVATE(dir)) {
  315. inode->i_flags |= S_PRIVATE;
  316. goto apply_umask;
  317. }
  318. acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
  319. if (IS_ERR(acl)) {
  320. if (PTR_ERR(acl) == -ENODATA)
  321. goto apply_umask;
  322. return PTR_ERR(acl);
  323. }
  324. if (acl) {
  325. struct posix_acl *acl_copy;
  326. mode_t mode = inode->i_mode;
  327. int need_acl;
  328. /* Copy the default ACL to the default ACL of a new directory */
  329. if (S_ISDIR(inode->i_mode)) {
  330. err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  331. if (err)
  332. goto cleanup;
  333. }
  334. /* Now we reconcile the new ACL and the mode,
  335. potentially modifying both */
  336. acl_copy = posix_acl_clone(acl, GFP_NOFS);
  337. if (!acl_copy) {
  338. err = -ENOMEM;
  339. goto cleanup;
  340. }
  341. need_acl = posix_acl_create_masq(acl_copy, &mode);
  342. if (need_acl >= 0) {
  343. if (mode != inode->i_mode) {
  344. inode->i_mode = mode;
  345. }
  346. /* If we need an ACL.. */
  347. if (need_acl > 0) {
  348. err =
  349. reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
  350. acl_copy);
  351. if (err)
  352. goto cleanup_copy;
  353. }
  354. }
  355. cleanup_copy:
  356. posix_acl_release(acl_copy);
  357. cleanup:
  358. posix_acl_release(acl);
  359. } else {
  360. apply_umask:
  361. /* no ACL, apply umask */
  362. inode->i_mode &= ~current->fs->umask;
  363. }
  364. return err;
  365. }
  366. /* Looks up and caches the result of the default ACL.
  367. * We do this so that we don't need to carry the xattr_sem into
  368. * reiserfs_new_inode if we don't need to */
  369. int reiserfs_cache_default_acl(struct inode *inode)
  370. {
  371. int ret = 0;
  372. if (reiserfs_posixacl(inode->i_sb) && !IS_PRIVATE(inode)) {
  373. struct posix_acl *acl;
  374. acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
  375. ret = (acl && !IS_ERR(acl));
  376. if (ret)
  377. posix_acl_release(acl);
  378. }
  379. return ret;
  380. }
  381. int reiserfs_acl_chmod(struct inode *inode)
  382. {
  383. struct posix_acl *acl, *clone;
  384. int error;
  385. if (S_ISLNK(inode->i_mode))
  386. return -EOPNOTSUPP;
  387. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  388. !reiserfs_posixacl(inode->i_sb)) {
  389. return 0;
  390. }
  391. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  392. if (!acl)
  393. return 0;
  394. if (IS_ERR(acl))
  395. return PTR_ERR(acl);
  396. clone = posix_acl_clone(acl, GFP_NOFS);
  397. posix_acl_release(acl);
  398. if (!clone)
  399. return -ENOMEM;
  400. error = posix_acl_chmod_masq(clone, inode->i_mode);
  401. if (!error)
  402. error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  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 size_t posix_acl_access_list(struct inode *inode, char *list,
  423. size_t list_size, const char *name,
  424. size_t name_len)
  425. {
  426. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  427. if (!reiserfs_posixacl(inode->i_sb))
  428. return 0;
  429. if (list && size <= list_size)
  430. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  431. return size;
  432. }
  433. struct xattr_handler reiserfs_posix_acl_access_handler = {
  434. .prefix = POSIX_ACL_XATTR_ACCESS,
  435. .get = posix_acl_access_get,
  436. .set = posix_acl_access_set,
  437. .list = posix_acl_access_list,
  438. };
  439. static int
  440. posix_acl_default_get(struct inode *inode, const char *name,
  441. void *buffer, size_t size)
  442. {
  443. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  444. return -EINVAL;
  445. return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  446. }
  447. static int
  448. posix_acl_default_set(struct inode *inode, const char *name,
  449. const void *value, size_t size, int flags)
  450. {
  451. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  452. return -EINVAL;
  453. return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  454. }
  455. static size_t posix_acl_default_list(struct inode *inode, char *list,
  456. size_t list_size, const char *name,
  457. size_t name_len)
  458. {
  459. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  460. if (!reiserfs_posixacl(inode->i_sb))
  461. return 0;
  462. if (list && size <= list_size)
  463. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  464. return size;
  465. }
  466. struct xattr_handler reiserfs_posix_acl_default_handler = {
  467. .prefix = POSIX_ACL_XATTR_DEFAULT,
  468. .get = posix_acl_default_get,
  469. .set = posix_acl_default_set,
  470. .list = posix_acl_default_list,
  471. };