xattr_acl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 reiserfs_transaction_handle *th,
  13. struct inode *inode, int type,
  14. struct posix_acl *acl);
  15. static int
  16. xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
  17. {
  18. struct posix_acl *acl;
  19. int error, error2;
  20. struct reiserfs_transaction_handle th;
  21. size_t jcreate_blocks;
  22. if (!reiserfs_posixacl(inode->i_sb))
  23. return -EOPNOTSUPP;
  24. if (!is_owner_or_cap(inode))
  25. return -EPERM;
  26. if (value) {
  27. acl = posix_acl_from_xattr(value, size);
  28. if (IS_ERR(acl)) {
  29. return PTR_ERR(acl);
  30. } else if (acl) {
  31. error = posix_acl_valid(acl);
  32. if (error)
  33. goto release_and_out;
  34. }
  35. } else
  36. acl = NULL;
  37. /* Pessimism: We can't assume that anything from the xattr root up
  38. * has been created. */
  39. jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
  40. reiserfs_xattr_nblocks(inode, size) * 2;
  41. reiserfs_write_lock(inode->i_sb);
  42. error = journal_begin(&th, inode->i_sb, jcreate_blocks);
  43. if (error == 0) {
  44. error = reiserfs_set_acl(&th, inode, type, acl);
  45. error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
  46. if (error2)
  47. error = error2;
  48. }
  49. reiserfs_write_unlock(inode->i_sb);
  50. release_and_out:
  51. posix_acl_release(acl);
  52. return error;
  53. }
  54. static int
  55. xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
  56. {
  57. struct posix_acl *acl;
  58. int error;
  59. if (!reiserfs_posixacl(inode->i_sb))
  60. return -EOPNOTSUPP;
  61. acl = reiserfs_get_acl(inode, type);
  62. if (IS_ERR(acl))
  63. return PTR_ERR(acl);
  64. if (acl == NULL)
  65. return -ENODATA;
  66. error = posix_acl_to_xattr(acl, buffer, size);
  67. posix_acl_release(acl);
  68. return error;
  69. }
  70. /*
  71. * Convert from filesystem to in-memory representation.
  72. */
  73. static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
  74. {
  75. const char *end = (char *)value + size;
  76. int n, count;
  77. struct posix_acl *acl;
  78. if (!value)
  79. return NULL;
  80. if (size < sizeof(reiserfs_acl_header))
  81. return ERR_PTR(-EINVAL);
  82. if (((reiserfs_acl_header *) value)->a_version !=
  83. cpu_to_le32(REISERFS_ACL_VERSION))
  84. return ERR_PTR(-EINVAL);
  85. value = (char *)value + sizeof(reiserfs_acl_header);
  86. count = reiserfs_acl_count(size);
  87. if (count < 0)
  88. return ERR_PTR(-EINVAL);
  89. if (count == 0)
  90. return NULL;
  91. acl = posix_acl_alloc(count, GFP_NOFS);
  92. if (!acl)
  93. return ERR_PTR(-ENOMEM);
  94. for (n = 0; n < count; n++) {
  95. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
  96. if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
  97. goto fail;
  98. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  99. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  100. switch (acl->a_entries[n].e_tag) {
  101. case ACL_USER_OBJ:
  102. case ACL_GROUP_OBJ:
  103. case ACL_MASK:
  104. case ACL_OTHER:
  105. value = (char *)value +
  106. sizeof(reiserfs_acl_entry_short);
  107. acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
  108. break;
  109. case ACL_USER:
  110. case ACL_GROUP:
  111. value = (char *)value + sizeof(reiserfs_acl_entry);
  112. if ((char *)value > end)
  113. goto fail;
  114. acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
  115. break;
  116. default:
  117. goto fail;
  118. }
  119. }
  120. if (value != end)
  121. goto fail;
  122. return acl;
  123. fail:
  124. posix_acl_release(acl);
  125. return ERR_PTR(-EINVAL);
  126. }
  127. /*
  128. * Convert from in-memory to filesystem representation.
  129. */
  130. static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
  131. {
  132. reiserfs_acl_header *ext_acl;
  133. char *e;
  134. int n;
  135. *size = reiserfs_acl_size(acl->a_count);
  136. ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
  137. acl->a_count *
  138. sizeof(reiserfs_acl_entry),
  139. GFP_NOFS);
  140. if (!ext_acl)
  141. return ERR_PTR(-ENOMEM);
  142. ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
  143. e = (char *)ext_acl + sizeof(reiserfs_acl_header);
  144. for (n = 0; n < acl->a_count; n++) {
  145. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
  146. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  147. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  148. switch (acl->a_entries[n].e_tag) {
  149. case ACL_USER:
  150. case ACL_GROUP:
  151. entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
  152. e += sizeof(reiserfs_acl_entry);
  153. break;
  154. case ACL_USER_OBJ:
  155. case ACL_GROUP_OBJ:
  156. case ACL_MASK:
  157. case ACL_OTHER:
  158. e += sizeof(reiserfs_acl_entry_short);
  159. break;
  160. default:
  161. goto fail;
  162. }
  163. }
  164. return (char *)ext_acl;
  165. fail:
  166. kfree(ext_acl);
  167. return ERR_PTR(-EINVAL);
  168. }
  169. /*
  170. * Inode operation get_posix_acl().
  171. *
  172. * inode->i_mutex: down
  173. * BKL held [before 2.5.x]
  174. */
  175. struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
  176. {
  177. char *name, *value;
  178. struct posix_acl *acl;
  179. int size;
  180. int retval;
  181. acl = get_cached_acl(inode, type);
  182. if (acl != ACL_NOT_CACHED)
  183. return acl;
  184. switch (type) {
  185. case ACL_TYPE_ACCESS:
  186. name = POSIX_ACL_XATTR_ACCESS;
  187. break;
  188. case ACL_TYPE_DEFAULT:
  189. name = POSIX_ACL_XATTR_DEFAULT;
  190. break;
  191. default:
  192. BUG();
  193. }
  194. size = reiserfs_xattr_get(inode, name, NULL, 0);
  195. if (size < 0) {
  196. if (size == -ENODATA || size == -ENOSYS) {
  197. set_cached_acl(inode, type, NULL);
  198. return NULL;
  199. }
  200. return ERR_PTR(size);
  201. }
  202. value = kmalloc(size, GFP_NOFS);
  203. if (!value)
  204. return ERR_PTR(-ENOMEM);
  205. retval = reiserfs_xattr_get(inode, name, value, size);
  206. if (retval == -ENODATA || retval == -ENOSYS) {
  207. /* This shouldn't actually happen as it should have
  208. been caught above.. but just in case */
  209. acl = NULL;
  210. } else if (retval < 0) {
  211. acl = ERR_PTR(retval);
  212. } else {
  213. acl = posix_acl_from_disk(value, retval);
  214. }
  215. if (!IS_ERR(acl))
  216. set_cached_acl(inode, type, acl);
  217. kfree(value);
  218. return acl;
  219. }
  220. /*
  221. * Inode operation set_posix_acl().
  222. *
  223. * inode->i_mutex: down
  224. * BKL held [before 2.5.x]
  225. */
  226. static int
  227. reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
  228. int type, struct posix_acl *acl)
  229. {
  230. char *name;
  231. void *value = NULL;
  232. size_t size = 0;
  233. int error;
  234. if (S_ISLNK(inode->i_mode))
  235. return -EOPNOTSUPP;
  236. switch (type) {
  237. case ACL_TYPE_ACCESS:
  238. name = POSIX_ACL_XATTR_ACCESS;
  239. if (acl) {
  240. mode_t mode = inode->i_mode;
  241. error = posix_acl_equiv_mode(acl, &mode);
  242. if (error < 0)
  243. return error;
  244. else {
  245. inode->i_mode = mode;
  246. if (error == 0)
  247. acl = NULL;
  248. }
  249. }
  250. break;
  251. case ACL_TYPE_DEFAULT:
  252. name = POSIX_ACL_XATTR_DEFAULT;
  253. if (!S_ISDIR(inode->i_mode))
  254. return acl ? -EACCES : 0;
  255. break;
  256. default:
  257. return -EINVAL;
  258. }
  259. if (acl) {
  260. value = posix_acl_to_disk(acl, &size);
  261. if (IS_ERR(value))
  262. return (int)PTR_ERR(value);
  263. }
  264. error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
  265. /*
  266. * Ensure that the inode gets dirtied if we're only using
  267. * the mode bits and an old ACL didn't exist. We don't need
  268. * to check if the inode is hashed here since we won't get
  269. * called by reiserfs_inherit_default_acl().
  270. */
  271. if (error == -ENODATA) {
  272. error = 0;
  273. if (type == ACL_TYPE_ACCESS) {
  274. inode->i_ctime = CURRENT_TIME_SEC;
  275. mark_inode_dirty(inode);
  276. }
  277. }
  278. kfree(value);
  279. if (!error)
  280. set_cached_acl(inode, type, acl);
  281. return error;
  282. }
  283. /* dir->i_mutex: locked,
  284. * inode is new and not released into the wild yet */
  285. int
  286. reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
  287. struct inode *dir, struct dentry *dentry,
  288. struct inode *inode)
  289. {
  290. struct posix_acl *acl;
  291. int err = 0;
  292. /* ACLs only get applied to files and directories */
  293. if (S_ISLNK(inode->i_mode))
  294. return 0;
  295. /* ACLs can only be used on "new" objects, so if it's an old object
  296. * there is nothing to inherit from */
  297. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  298. goto apply_umask;
  299. /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  300. * would be useless since permissions are ignored, and a pain because
  301. * it introduces locking cycles */
  302. if (IS_PRIVATE(dir)) {
  303. inode->i_flags |= S_PRIVATE;
  304. goto apply_umask;
  305. }
  306. acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
  307. if (IS_ERR(acl))
  308. return PTR_ERR(acl);
  309. if (acl) {
  310. struct posix_acl *acl_copy;
  311. mode_t mode = inode->i_mode;
  312. int need_acl;
  313. /* Copy the default ACL to the default ACL of a new directory */
  314. if (S_ISDIR(inode->i_mode)) {
  315. err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
  316. 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(th, inode,
  335. ACL_TYPE_ACCESS,
  336. acl_copy);
  337. if (err)
  338. goto cleanup_copy;
  339. }
  340. }
  341. cleanup_copy:
  342. posix_acl_release(acl_copy);
  343. cleanup:
  344. posix_acl_release(acl);
  345. } else {
  346. apply_umask:
  347. /* no ACL, apply umask */
  348. inode->i_mode &= ~current_umask();
  349. }
  350. return err;
  351. }
  352. /* This is used to cache the default acl before a new object is created.
  353. * The biggest reason for this is to get an idea of how many blocks will
  354. * actually be required for the create operation if we must inherit an ACL.
  355. * An ACL write can add up to 3 object creations and an additional file write
  356. * so we'd prefer not to reserve that many blocks in the journal if we can.
  357. * It also has the advantage of not loading the ACL with a transaction open,
  358. * this may seem silly, but if the owner of the directory is doing the
  359. * creation, the ACL may not be loaded since the permissions wouldn't require
  360. * it.
  361. * We return the number of blocks required for the transaction.
  362. */
  363. int reiserfs_cache_default_acl(struct inode *inode)
  364. {
  365. struct posix_acl *acl;
  366. int nblocks = 0;
  367. if (IS_PRIVATE(inode))
  368. return 0;
  369. acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
  370. if (acl && !IS_ERR(acl)) {
  371. int size = reiserfs_acl_size(acl->a_count);
  372. /* Other xattrs can be created during inode creation. We don't
  373. * want to claim too many blocks, so we check to see if we
  374. * we need to create the tree to the xattrs, and then we
  375. * just want two files. */
  376. nblocks = reiserfs_xattr_jcreate_nblocks(inode);
  377. nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
  378. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  379. /* We need to account for writes + bitmaps for two files */
  380. nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
  381. posix_acl_release(acl);
  382. }
  383. return nblocks;
  384. }
  385. int reiserfs_acl_chmod(struct inode *inode)
  386. {
  387. struct posix_acl *acl, *clone;
  388. int error;
  389. if (S_ISLNK(inode->i_mode))
  390. return -EOPNOTSUPP;
  391. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  392. !reiserfs_posixacl(inode->i_sb)) {
  393. return 0;
  394. }
  395. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  396. if (!acl)
  397. return 0;
  398. if (IS_ERR(acl))
  399. return PTR_ERR(acl);
  400. clone = posix_acl_clone(acl, GFP_NOFS);
  401. posix_acl_release(acl);
  402. if (!clone)
  403. return -ENOMEM;
  404. error = posix_acl_chmod_masq(clone, inode->i_mode);
  405. if (!error) {
  406. struct reiserfs_transaction_handle th;
  407. size_t size = reiserfs_xattr_nblocks(inode,
  408. reiserfs_acl_size(clone->a_count));
  409. reiserfs_write_lock(inode->i_sb);
  410. error = journal_begin(&th, inode->i_sb, size * 2);
  411. if (!error) {
  412. int error2;
  413. error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS,
  414. clone);
  415. error2 = journal_end(&th, inode->i_sb, size * 2);
  416. if (error2)
  417. error = error2;
  418. }
  419. reiserfs_write_unlock(inode->i_sb);
  420. }
  421. posix_acl_release(clone);
  422. return error;
  423. }
  424. static int
  425. posix_acl_access_get(struct inode *inode, const char *name,
  426. void *buffer, size_t size)
  427. {
  428. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  429. return -EINVAL;
  430. return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  431. }
  432. static int
  433. posix_acl_access_set(struct inode *inode, const char *name,
  434. const void *value, size_t size, int flags)
  435. {
  436. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  437. return -EINVAL;
  438. return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  439. }
  440. static size_t posix_acl_access_list(struct inode *inode, char *list,
  441. size_t list_size, const char *name,
  442. size_t name_len)
  443. {
  444. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  445. if (!reiserfs_posixacl(inode->i_sb))
  446. return 0;
  447. if (list && size <= list_size)
  448. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  449. return size;
  450. }
  451. struct xattr_handler reiserfs_posix_acl_access_handler = {
  452. .prefix = POSIX_ACL_XATTR_ACCESS,
  453. .get = posix_acl_access_get,
  454. .set = posix_acl_access_set,
  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 size_t posix_acl_default_list(struct inode *inode, char *list,
  474. size_t list_size, const char *name,
  475. size_t name_len)
  476. {
  477. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  478. if (!reiserfs_posixacl(inode->i_sb))
  479. return 0;
  480. if (list && size <= list_size)
  481. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  482. return size;
  483. }
  484. struct xattr_handler reiserfs_posix_acl_default_handler = {
  485. .prefix = POSIX_ACL_XATTR_DEFAULT,
  486. .get = posix_acl_default_get,
  487. .set = posix_acl_default_set,
  488. .list = posix_acl_default_list,
  489. };