xattr_acl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #include <linux/capability.h>
  2. #include <linux/fs.h>
  3. #include <linux/posix_acl.h>
  4. #include "reiserfs.h"
  5. #include <linux/errno.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/xattr.h>
  8. #include <linux/slab.h>
  9. #include <linux/posix_acl_xattr.h>
  10. #include "xattr.h"
  11. #include "acl.h"
  12. #include <asm/uaccess.h>
  13. static int reiserfs_set_acl(struct reiserfs_transaction_handle *th,
  14. struct inode *inode, int type,
  15. struct posix_acl *acl);
  16. static int
  17. posix_acl_set(struct dentry *dentry, const char *name, const void *value,
  18. size_t size, int flags, int type)
  19. {
  20. struct inode *inode = dentry->d_inode;
  21. struct posix_acl *acl;
  22. int error, error2;
  23. struct reiserfs_transaction_handle th;
  24. size_t jcreate_blocks;
  25. if (!reiserfs_posixacl(inode->i_sb))
  26. return -EOPNOTSUPP;
  27. if (!inode_owner_or_capable(inode))
  28. return -EPERM;
  29. if (value) {
  30. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  31. if (IS_ERR(acl)) {
  32. return PTR_ERR(acl);
  33. } else if (acl) {
  34. error = posix_acl_valid(acl);
  35. if (error)
  36. goto release_and_out;
  37. }
  38. } else
  39. acl = NULL;
  40. /* Pessimism: We can't assume that anything from the xattr root up
  41. * has been created. */
  42. jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
  43. reiserfs_xattr_nblocks(inode, size) * 2;
  44. reiserfs_write_lock(inode->i_sb);
  45. error = journal_begin(&th, inode->i_sb, jcreate_blocks);
  46. reiserfs_write_unlock(inode->i_sb);
  47. if (error == 0) {
  48. error = reiserfs_set_acl(&th, inode, type, acl);
  49. reiserfs_write_lock(inode->i_sb);
  50. error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
  51. reiserfs_write_unlock(inode->i_sb);
  52. if (error2)
  53. error = error2;
  54. }
  55. release_and_out:
  56. posix_acl_release(acl);
  57. return error;
  58. }
  59. static int
  60. posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
  61. size_t size, int type)
  62. {
  63. struct posix_acl *acl;
  64. int error;
  65. if (!reiserfs_posixacl(dentry->d_sb))
  66. return -EOPNOTSUPP;
  67. acl = reiserfs_get_acl(dentry->d_inode, type);
  68. if (IS_ERR(acl))
  69. return PTR_ERR(acl);
  70. if (acl == NULL)
  71. return -ENODATA;
  72. error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  73. posix_acl_release(acl);
  74. return error;
  75. }
  76. /*
  77. * Convert from filesystem to in-memory representation.
  78. */
  79. static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
  80. {
  81. const char *end = (char *)value + size;
  82. int n, count;
  83. struct posix_acl *acl;
  84. if (!value)
  85. return NULL;
  86. if (size < sizeof(reiserfs_acl_header))
  87. return ERR_PTR(-EINVAL);
  88. if (((reiserfs_acl_header *) value)->a_version !=
  89. cpu_to_le32(REISERFS_ACL_VERSION))
  90. return ERR_PTR(-EINVAL);
  91. value = (char *)value + sizeof(reiserfs_acl_header);
  92. count = reiserfs_acl_count(size);
  93. if (count < 0)
  94. return ERR_PTR(-EINVAL);
  95. if (count == 0)
  96. return NULL;
  97. acl = posix_acl_alloc(count, GFP_NOFS);
  98. if (!acl)
  99. return ERR_PTR(-ENOMEM);
  100. for (n = 0; n < count; n++) {
  101. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
  102. if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
  103. goto fail;
  104. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  105. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  106. switch (acl->a_entries[n].e_tag) {
  107. case ACL_USER_OBJ:
  108. case ACL_GROUP_OBJ:
  109. case ACL_MASK:
  110. case ACL_OTHER:
  111. value = (char *)value +
  112. sizeof(reiserfs_acl_entry_short);
  113. break;
  114. case ACL_USER:
  115. value = (char *)value + sizeof(reiserfs_acl_entry);
  116. if ((char *)value > end)
  117. goto fail;
  118. acl->a_entries[n].e_uid =
  119. make_kuid(&init_user_ns,
  120. le32_to_cpu(entry->e_id));
  121. break;
  122. case ACL_GROUP:
  123. value = (char *)value + sizeof(reiserfs_acl_entry);
  124. if ((char *)value > end)
  125. goto fail;
  126. acl->a_entries[n].e_gid =
  127. make_kgid(&init_user_ns,
  128. le32_to_cpu(entry->e_id));
  129. break;
  130. default:
  131. goto fail;
  132. }
  133. }
  134. if (value != end)
  135. goto fail;
  136. return acl;
  137. fail:
  138. posix_acl_release(acl);
  139. return ERR_PTR(-EINVAL);
  140. }
  141. /*
  142. * Convert from in-memory to filesystem representation.
  143. */
  144. static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
  145. {
  146. reiserfs_acl_header *ext_acl;
  147. char *e;
  148. int n;
  149. *size = reiserfs_acl_size(acl->a_count);
  150. ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
  151. acl->a_count *
  152. sizeof(reiserfs_acl_entry),
  153. GFP_NOFS);
  154. if (!ext_acl)
  155. return ERR_PTR(-ENOMEM);
  156. ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
  157. e = (char *)ext_acl + sizeof(reiserfs_acl_header);
  158. for (n = 0; n < acl->a_count; n++) {
  159. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  160. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
  161. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  162. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  163. switch (acl->a_entries[n].e_tag) {
  164. case ACL_USER:
  165. entry->e_id = cpu_to_le32(
  166. from_kuid(&init_user_ns, acl_e->e_uid));
  167. e += sizeof(reiserfs_acl_entry);
  168. break;
  169. case ACL_GROUP:
  170. entry->e_id = cpu_to_le32(
  171. from_kgid(&init_user_ns, acl_e->e_gid));
  172. e += sizeof(reiserfs_acl_entry);
  173. break;
  174. case ACL_USER_OBJ:
  175. case ACL_GROUP_OBJ:
  176. case ACL_MASK:
  177. case ACL_OTHER:
  178. e += sizeof(reiserfs_acl_entry_short);
  179. break;
  180. default:
  181. goto fail;
  182. }
  183. }
  184. return (char *)ext_acl;
  185. fail:
  186. kfree(ext_acl);
  187. return ERR_PTR(-EINVAL);
  188. }
  189. /*
  190. * Inode operation get_posix_acl().
  191. *
  192. * inode->i_mutex: down
  193. * BKL held [before 2.5.x]
  194. */
  195. struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
  196. {
  197. char *name, *value;
  198. struct posix_acl *acl;
  199. int size;
  200. int retval;
  201. acl = get_cached_acl(inode, type);
  202. if (acl != ACL_NOT_CACHED)
  203. return acl;
  204. switch (type) {
  205. case ACL_TYPE_ACCESS:
  206. name = POSIX_ACL_XATTR_ACCESS;
  207. break;
  208. case ACL_TYPE_DEFAULT:
  209. name = POSIX_ACL_XATTR_DEFAULT;
  210. break;
  211. default:
  212. BUG();
  213. }
  214. size = reiserfs_xattr_get(inode, name, NULL, 0);
  215. if (size < 0) {
  216. if (size == -ENODATA || size == -ENOSYS) {
  217. set_cached_acl(inode, type, NULL);
  218. return NULL;
  219. }
  220. return ERR_PTR(size);
  221. }
  222. value = kmalloc(size, GFP_NOFS);
  223. if (!value)
  224. return ERR_PTR(-ENOMEM);
  225. retval = reiserfs_xattr_get(inode, name, value, size);
  226. if (retval == -ENODATA || retval == -ENOSYS) {
  227. /* This shouldn't actually happen as it should have
  228. been caught above.. but just in case */
  229. acl = NULL;
  230. } else if (retval < 0) {
  231. acl = ERR_PTR(retval);
  232. } else {
  233. acl = posix_acl_from_disk(value, retval);
  234. }
  235. if (!IS_ERR(acl))
  236. set_cached_acl(inode, type, acl);
  237. kfree(value);
  238. return acl;
  239. }
  240. /*
  241. * Inode operation set_posix_acl().
  242. *
  243. * inode->i_mutex: down
  244. * BKL held [before 2.5.x]
  245. */
  246. static int
  247. reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
  248. int type, struct posix_acl *acl)
  249. {
  250. char *name;
  251. void *value = NULL;
  252. size_t size = 0;
  253. int error;
  254. if (S_ISLNK(inode->i_mode))
  255. return -EOPNOTSUPP;
  256. switch (type) {
  257. case ACL_TYPE_ACCESS:
  258. name = POSIX_ACL_XATTR_ACCESS;
  259. if (acl) {
  260. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  261. if (error < 0)
  262. return error;
  263. else {
  264. if (error == 0)
  265. acl = NULL;
  266. }
  267. }
  268. break;
  269. case ACL_TYPE_DEFAULT:
  270. name = POSIX_ACL_XATTR_DEFAULT;
  271. if (!S_ISDIR(inode->i_mode))
  272. return acl ? -EACCES : 0;
  273. break;
  274. default:
  275. return -EINVAL;
  276. }
  277. if (acl) {
  278. value = posix_acl_to_disk(acl, &size);
  279. if (IS_ERR(value))
  280. return (int)PTR_ERR(value);
  281. }
  282. error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
  283. /*
  284. * Ensure that the inode gets dirtied if we're only using
  285. * the mode bits and an old ACL didn't exist. We don't need
  286. * to check if the inode is hashed here since we won't get
  287. * called by reiserfs_inherit_default_acl().
  288. */
  289. if (error == -ENODATA) {
  290. error = 0;
  291. if (type == ACL_TYPE_ACCESS) {
  292. inode->i_ctime = CURRENT_TIME_SEC;
  293. mark_inode_dirty(inode);
  294. }
  295. }
  296. kfree(value);
  297. if (!error)
  298. set_cached_acl(inode, type, acl);
  299. return error;
  300. }
  301. /* dir->i_mutex: locked,
  302. * inode is new and not released into the wild yet */
  303. int
  304. reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
  305. struct inode *dir, struct dentry *dentry,
  306. struct inode *inode)
  307. {
  308. struct posix_acl *acl;
  309. int err = 0;
  310. /* ACLs only get applied to files and directories */
  311. if (S_ISLNK(inode->i_mode))
  312. return 0;
  313. /* ACLs can only be used on "new" objects, so if it's an old object
  314. * there is nothing to inherit from */
  315. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  316. goto apply_umask;
  317. /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  318. * would be useless since permissions are ignored, and a pain because
  319. * it introduces locking cycles */
  320. if (IS_PRIVATE(dir)) {
  321. inode->i_flags |= S_PRIVATE;
  322. goto apply_umask;
  323. }
  324. acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
  325. if (IS_ERR(acl))
  326. return PTR_ERR(acl);
  327. if (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(th, inode, ACL_TYPE_DEFAULT,
  331. acl);
  332. if (err)
  333. goto cleanup;
  334. }
  335. /* Now we reconcile the new ACL and the mode,
  336. potentially modifying both */
  337. err = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
  338. if (err < 0)
  339. return err;
  340. /* If we need an ACL.. */
  341. if (err > 0)
  342. err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl);
  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. /*
  386. * Called under i_mutex
  387. */
  388. int reiserfs_acl_chmod(struct inode *inode)
  389. {
  390. struct reiserfs_transaction_handle th;
  391. struct posix_acl *acl;
  392. size_t size;
  393. int error;
  394. if (IS_PRIVATE(inode))
  395. return 0;
  396. if (S_ISLNK(inode->i_mode))
  397. return -EOPNOTSUPP;
  398. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  399. !reiserfs_posixacl(inode->i_sb)) {
  400. return 0;
  401. }
  402. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  403. if (!acl)
  404. return 0;
  405. if (IS_ERR(acl))
  406. return PTR_ERR(acl);
  407. error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode);
  408. if (error)
  409. return error;
  410. size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count));
  411. reiserfs_write_lock(inode->i_sb);
  412. error = journal_begin(&th, inode->i_sb, size * 2);
  413. reiserfs_write_unlock(inode->i_sb);
  414. if (!error) {
  415. int error2;
  416. error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl);
  417. reiserfs_write_lock(inode->i_sb);
  418. error2 = journal_end(&th, inode->i_sb, size * 2);
  419. reiserfs_write_unlock(inode->i_sb);
  420. if (error2)
  421. error = error2;
  422. }
  423. posix_acl_release(acl);
  424. return error;
  425. }
  426. static size_t posix_acl_access_list(struct dentry *dentry, char *list,
  427. size_t list_size, const char *name,
  428. size_t name_len, int type)
  429. {
  430. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  431. if (!reiserfs_posixacl(dentry->d_sb))
  432. return 0;
  433. if (list && size <= list_size)
  434. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  435. return size;
  436. }
  437. const struct xattr_handler reiserfs_posix_acl_access_handler = {
  438. .prefix = POSIX_ACL_XATTR_ACCESS,
  439. .flags = ACL_TYPE_ACCESS,
  440. .get = posix_acl_get,
  441. .set = posix_acl_set,
  442. .list = posix_acl_access_list,
  443. };
  444. static size_t posix_acl_default_list(struct dentry *dentry, char *list,
  445. size_t list_size, const char *name,
  446. size_t name_len, int type)
  447. {
  448. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  449. if (!reiserfs_posixacl(dentry->d_sb))
  450. return 0;
  451. if (list && size <= list_size)
  452. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  453. return size;
  454. }
  455. const struct xattr_handler reiserfs_posix_acl_default_handler = {
  456. .prefix = POSIX_ACL_XATTR_DEFAULT,
  457. .flags = ACL_TYPE_DEFAULT,
  458. .get = posix_acl_get,
  459. .set = posix_acl_set,
  460. .list = posix_acl_default_list,
  461. };