xattr_acl.c 14 KB

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