xattr_acl.c 13 KB

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