xattr_acl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. static inline void iset_acl(struct inode *inode, struct posix_acl **i_acl,
  170. struct posix_acl *acl)
  171. {
  172. spin_lock(&inode->i_lock);
  173. if (*i_acl != ACL_NOT_CACHED)
  174. posix_acl_release(*i_acl);
  175. *i_acl = posix_acl_dup(acl);
  176. spin_unlock(&inode->i_lock);
  177. }
  178. static inline struct posix_acl *iget_acl(struct inode *inode,
  179. struct posix_acl **i_acl)
  180. {
  181. struct posix_acl *acl = ACL_NOT_CACHED;
  182. spin_lock(&inode->i_lock);
  183. if (*i_acl != ACL_NOT_CACHED)
  184. acl = posix_acl_dup(*i_acl);
  185. spin_unlock(&inode->i_lock);
  186. return acl;
  187. }
  188. /*
  189. * Inode operation get_posix_acl().
  190. *
  191. * inode->i_mutex: down
  192. * BKL held [before 2.5.x]
  193. */
  194. struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
  195. {
  196. char *name, *value;
  197. struct posix_acl *acl, **p_acl;
  198. int size;
  199. int retval;
  200. switch (type) {
  201. case ACL_TYPE_ACCESS:
  202. name = POSIX_ACL_XATTR_ACCESS;
  203. p_acl = &inode->i_acl;
  204. break;
  205. case ACL_TYPE_DEFAULT:
  206. name = POSIX_ACL_XATTR_DEFAULT;
  207. p_acl = &inode->i_default_acl;
  208. break;
  209. default:
  210. return ERR_PTR(-EINVAL);
  211. }
  212. acl = iget_acl(inode, p_acl);
  213. if (acl != ACL_NOT_CACHED)
  214. return acl;
  215. size = reiserfs_xattr_get(inode, name, NULL, 0);
  216. if (size < 0) {
  217. if (size == -ENODATA || size == -ENOSYS) {
  218. *p_acl = NULL;
  219. return NULL;
  220. }
  221. return ERR_PTR(size);
  222. }
  223. value = kmalloc(size, GFP_NOFS);
  224. if (!value)
  225. return ERR_PTR(-ENOMEM);
  226. retval = reiserfs_xattr_get(inode, name, value, size);
  227. if (retval == -ENODATA || retval == -ENOSYS) {
  228. /* This shouldn't actually happen as it should have
  229. been caught above.. but just in case */
  230. acl = NULL;
  231. *p_acl = acl;
  232. } else if (retval < 0) {
  233. acl = ERR_PTR(retval);
  234. } else {
  235. acl = posix_acl_from_disk(value, retval);
  236. if (!IS_ERR(acl))
  237. iset_acl(inode, p_acl, acl);
  238. }
  239. kfree(value);
  240. return acl;
  241. }
  242. /*
  243. * Inode operation set_posix_acl().
  244. *
  245. * inode->i_mutex: down
  246. * BKL held [before 2.5.x]
  247. */
  248. static int
  249. reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
  250. int type, struct posix_acl *acl)
  251. {
  252. char *name;
  253. void *value = NULL;
  254. struct posix_acl **p_acl;
  255. size_t size = 0;
  256. int error;
  257. if (S_ISLNK(inode->i_mode))
  258. return -EOPNOTSUPP;
  259. switch (type) {
  260. case ACL_TYPE_ACCESS:
  261. name = POSIX_ACL_XATTR_ACCESS;
  262. p_acl = &inode->i_acl;
  263. if (acl) {
  264. mode_t mode = inode->i_mode;
  265. error = posix_acl_equiv_mode(acl, &mode);
  266. if (error < 0)
  267. return error;
  268. else {
  269. inode->i_mode = mode;
  270. if (error == 0)
  271. acl = NULL;
  272. }
  273. }
  274. break;
  275. case ACL_TYPE_DEFAULT:
  276. name = POSIX_ACL_XATTR_DEFAULT;
  277. p_acl = &inode->i_default_acl;
  278. if (!S_ISDIR(inode->i_mode))
  279. return acl ? -EACCES : 0;
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. if (acl) {
  285. value = posix_acl_to_disk(acl, &size);
  286. if (IS_ERR(value))
  287. return (int)PTR_ERR(value);
  288. }
  289. error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
  290. /*
  291. * Ensure that the inode gets dirtied if we're only using
  292. * the mode bits and an old ACL didn't exist. We don't need
  293. * to check if the inode is hashed here since we won't get
  294. * called by reiserfs_inherit_default_acl().
  295. */
  296. if (error == -ENODATA) {
  297. error = 0;
  298. if (type == ACL_TYPE_ACCESS) {
  299. inode->i_ctime = CURRENT_TIME_SEC;
  300. mark_inode_dirty(inode);
  301. }
  302. }
  303. kfree(value);
  304. if (!error)
  305. iset_acl(inode, p_acl, acl);
  306. return error;
  307. }
  308. /* dir->i_mutex: locked,
  309. * inode is new and not released into the wild yet */
  310. int
  311. reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
  312. struct inode *dir, struct dentry *dentry,
  313. struct inode *inode)
  314. {
  315. struct posix_acl *acl;
  316. int err = 0;
  317. /* ACLs only get applied to files and directories */
  318. if (S_ISLNK(inode->i_mode))
  319. return 0;
  320. /* ACLs can only be used on "new" objects, so if it's an old object
  321. * there is nothing to inherit from */
  322. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  323. goto apply_umask;
  324. /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  325. * would be useless since permissions are ignored, and a pain because
  326. * it introduces locking cycles */
  327. if (IS_PRIVATE(dir)) {
  328. inode->i_flags |= S_PRIVATE;
  329. goto apply_umask;
  330. }
  331. acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
  332. if (IS_ERR(acl))
  333. return PTR_ERR(acl);
  334. if (acl) {
  335. struct posix_acl *acl_copy;
  336. mode_t mode = inode->i_mode;
  337. int need_acl;
  338. /* Copy the default ACL to the default ACL of a new directory */
  339. if (S_ISDIR(inode->i_mode)) {
  340. err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
  341. acl);
  342. if (err)
  343. goto cleanup;
  344. }
  345. /* Now we reconcile the new ACL and the mode,
  346. potentially modifying both */
  347. acl_copy = posix_acl_clone(acl, GFP_NOFS);
  348. if (!acl_copy) {
  349. err = -ENOMEM;
  350. goto cleanup;
  351. }
  352. need_acl = posix_acl_create_masq(acl_copy, &mode);
  353. if (need_acl >= 0) {
  354. if (mode != inode->i_mode) {
  355. inode->i_mode = mode;
  356. }
  357. /* If we need an ACL.. */
  358. if (need_acl > 0) {
  359. err = reiserfs_set_acl(th, inode,
  360. ACL_TYPE_ACCESS,
  361. acl_copy);
  362. if (err)
  363. goto cleanup_copy;
  364. }
  365. }
  366. cleanup_copy:
  367. posix_acl_release(acl_copy);
  368. cleanup:
  369. posix_acl_release(acl);
  370. } else {
  371. apply_umask:
  372. /* no ACL, apply umask */
  373. inode->i_mode &= ~current_umask();
  374. }
  375. return err;
  376. }
  377. /* This is used to cache the default acl before a new object is created.
  378. * The biggest reason for this is to get an idea of how many blocks will
  379. * actually be required for the create operation if we must inherit an ACL.
  380. * An ACL write can add up to 3 object creations and an additional file write
  381. * so we'd prefer not to reserve that many blocks in the journal if we can.
  382. * It also has the advantage of not loading the ACL with a transaction open,
  383. * this may seem silly, but if the owner of the directory is doing the
  384. * creation, the ACL may not be loaded since the permissions wouldn't require
  385. * it.
  386. * We return the number of blocks required for the transaction.
  387. */
  388. int reiserfs_cache_default_acl(struct inode *inode)
  389. {
  390. struct posix_acl *acl;
  391. int nblocks = 0;
  392. if (IS_PRIVATE(inode))
  393. return 0;
  394. acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
  395. if (acl && !IS_ERR(acl)) {
  396. int size = reiserfs_acl_size(acl->a_count);
  397. /* Other xattrs can be created during inode creation. We don't
  398. * want to claim too many blocks, so we check to see if we
  399. * we need to create the tree to the xattrs, and then we
  400. * just want two files. */
  401. nblocks = reiserfs_xattr_jcreate_nblocks(inode);
  402. nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
  403. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  404. /* We need to account for writes + bitmaps for two files */
  405. nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
  406. posix_acl_release(acl);
  407. }
  408. return nblocks;
  409. }
  410. int reiserfs_acl_chmod(struct inode *inode)
  411. {
  412. struct posix_acl *acl, *clone;
  413. int error;
  414. if (S_ISLNK(inode->i_mode))
  415. return -EOPNOTSUPP;
  416. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  417. !reiserfs_posixacl(inode->i_sb)) {
  418. return 0;
  419. }
  420. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  421. if (!acl)
  422. return 0;
  423. if (IS_ERR(acl))
  424. return PTR_ERR(acl);
  425. clone = posix_acl_clone(acl, GFP_NOFS);
  426. posix_acl_release(acl);
  427. if (!clone)
  428. return -ENOMEM;
  429. error = posix_acl_chmod_masq(clone, inode->i_mode);
  430. if (!error) {
  431. struct reiserfs_transaction_handle th;
  432. size_t size = reiserfs_xattr_nblocks(inode,
  433. reiserfs_acl_size(clone->a_count));
  434. reiserfs_write_lock(inode->i_sb);
  435. error = journal_begin(&th, inode->i_sb, size * 2);
  436. if (!error) {
  437. int error2;
  438. error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS,
  439. clone);
  440. error2 = journal_end(&th, inode->i_sb, size * 2);
  441. if (error2)
  442. error = error2;
  443. }
  444. reiserfs_write_unlock(inode->i_sb);
  445. }
  446. posix_acl_release(clone);
  447. return error;
  448. }
  449. static int
  450. posix_acl_access_get(struct inode *inode, const char *name,
  451. void *buffer, size_t size)
  452. {
  453. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  454. return -EINVAL;
  455. return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  456. }
  457. static int
  458. posix_acl_access_set(struct inode *inode, const char *name,
  459. const void *value, size_t size, int flags)
  460. {
  461. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  462. return -EINVAL;
  463. return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  464. }
  465. static size_t posix_acl_access_list(struct inode *inode, char *list,
  466. size_t list_size, const char *name,
  467. size_t name_len)
  468. {
  469. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  470. if (!reiserfs_posixacl(inode->i_sb))
  471. return 0;
  472. if (list && size <= list_size)
  473. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  474. return size;
  475. }
  476. struct xattr_handler reiserfs_posix_acl_access_handler = {
  477. .prefix = POSIX_ACL_XATTR_ACCESS,
  478. .get = posix_acl_access_get,
  479. .set = posix_acl_access_set,
  480. .list = posix_acl_access_list,
  481. };
  482. static int
  483. posix_acl_default_get(struct inode *inode, const char *name,
  484. void *buffer, size_t size)
  485. {
  486. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  487. return -EINVAL;
  488. return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  489. }
  490. static int
  491. posix_acl_default_set(struct inode *inode, const char *name,
  492. const void *value, size_t size, int flags)
  493. {
  494. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  495. return -EINVAL;
  496. return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  497. }
  498. static size_t posix_acl_default_list(struct inode *inode, char *list,
  499. size_t list_size, const char *name,
  500. size_t name_len)
  501. {
  502. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  503. if (!reiserfs_posixacl(inode->i_sb))
  504. return 0;
  505. if (list && size <= list_size)
  506. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  507. return size;
  508. }
  509. struct xattr_handler reiserfs_posix_acl_default_handler = {
  510. .prefix = POSIX_ACL_XATTR_DEFAULT,
  511. .get = posix_acl_default_get,
  512. .set = posix_acl_default_set,
  513. .list = posix_acl_default_list,
  514. };