xattr_acl.c 14 KB

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