xattr_acl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <linux/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. int
  17. reiserfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  18. {
  19. int error, error2;
  20. struct reiserfs_transaction_handle th;
  21. size_t jcreate_blocks;
  22. int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
  23. /*
  24. * Pessimism: We can't assume that anything from the xattr root up
  25. * has been created.
  26. */
  27. jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
  28. reiserfs_xattr_nblocks(inode, size) * 2;
  29. reiserfs_write_lock(inode->i_sb);
  30. error = journal_begin(&th, inode->i_sb, jcreate_blocks);
  31. reiserfs_write_unlock(inode->i_sb);
  32. if (error == 0) {
  33. if (type == ACL_TYPE_ACCESS && acl) {
  34. error = posix_acl_update_mode(inode, &inode->i_mode,
  35. &acl);
  36. if (error)
  37. goto unlock;
  38. }
  39. error = __reiserfs_set_acl(&th, inode, type, acl);
  40. unlock:
  41. reiserfs_write_lock(inode->i_sb);
  42. error2 = journal_end(&th);
  43. reiserfs_write_unlock(inode->i_sb);
  44. if (error2)
  45. error = error2;
  46. }
  47. return error;
  48. }
  49. /*
  50. * Convert from filesystem to in-memory representation.
  51. */
  52. static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
  53. {
  54. const char *end = (char *)value + size;
  55. int n, count;
  56. struct posix_acl *acl;
  57. if (!value)
  58. return NULL;
  59. if (size < sizeof(reiserfs_acl_header))
  60. return ERR_PTR(-EINVAL);
  61. if (((reiserfs_acl_header *) value)->a_version !=
  62. cpu_to_le32(REISERFS_ACL_VERSION))
  63. return ERR_PTR(-EINVAL);
  64. value = (char *)value + sizeof(reiserfs_acl_header);
  65. count = reiserfs_acl_count(size);
  66. if (count < 0)
  67. return ERR_PTR(-EINVAL);
  68. if (count == 0)
  69. return NULL;
  70. acl = posix_acl_alloc(count, GFP_NOFS);
  71. if (!acl)
  72. return ERR_PTR(-ENOMEM);
  73. for (n = 0; n < count; n++) {
  74. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
  75. if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
  76. goto fail;
  77. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  78. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  79. switch (acl->a_entries[n].e_tag) {
  80. case ACL_USER_OBJ:
  81. case ACL_GROUP_OBJ:
  82. case ACL_MASK:
  83. case ACL_OTHER:
  84. value = (char *)value +
  85. sizeof(reiserfs_acl_entry_short);
  86. break;
  87. case ACL_USER:
  88. value = (char *)value + sizeof(reiserfs_acl_entry);
  89. if ((char *)value > end)
  90. goto fail;
  91. acl->a_entries[n].e_uid =
  92. make_kuid(&init_user_ns,
  93. le32_to_cpu(entry->e_id));
  94. break;
  95. case ACL_GROUP:
  96. value = (char *)value + sizeof(reiserfs_acl_entry);
  97. if ((char *)value > end)
  98. goto fail;
  99. acl->a_entries[n].e_gid =
  100. make_kgid(&init_user_ns,
  101. le32_to_cpu(entry->e_id));
  102. break;
  103. default:
  104. goto fail;
  105. }
  106. }
  107. if (value != end)
  108. goto fail;
  109. return acl;
  110. fail:
  111. posix_acl_release(acl);
  112. return ERR_PTR(-EINVAL);
  113. }
  114. /*
  115. * Convert from in-memory to filesystem representation.
  116. */
  117. static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
  118. {
  119. reiserfs_acl_header *ext_acl;
  120. char *e;
  121. int n;
  122. *size = reiserfs_acl_size(acl->a_count);
  123. ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
  124. acl->a_count *
  125. sizeof(reiserfs_acl_entry),
  126. GFP_NOFS);
  127. if (!ext_acl)
  128. return ERR_PTR(-ENOMEM);
  129. ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
  130. e = (char *)ext_acl + sizeof(reiserfs_acl_header);
  131. for (n = 0; n < acl->a_count; n++) {
  132. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  133. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
  134. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  135. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  136. switch (acl->a_entries[n].e_tag) {
  137. case ACL_USER:
  138. entry->e_id = cpu_to_le32(
  139. from_kuid(&init_user_ns, acl_e->e_uid));
  140. e += sizeof(reiserfs_acl_entry);
  141. break;
  142. case ACL_GROUP:
  143. entry->e_id = cpu_to_le32(
  144. from_kgid(&init_user_ns, acl_e->e_gid));
  145. e += sizeof(reiserfs_acl_entry);
  146. break;
  147. case ACL_USER_OBJ:
  148. case ACL_GROUP_OBJ:
  149. case ACL_MASK:
  150. case ACL_OTHER:
  151. e += sizeof(reiserfs_acl_entry_short);
  152. break;
  153. default:
  154. goto fail;
  155. }
  156. }
  157. return (char *)ext_acl;
  158. fail:
  159. kfree(ext_acl);
  160. return ERR_PTR(-EINVAL);
  161. }
  162. /*
  163. * Inode operation get_posix_acl().
  164. *
  165. * inode->i_mutex: down
  166. * BKL held [before 2.5.x]
  167. */
  168. struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
  169. {
  170. char *name, *value;
  171. struct posix_acl *acl;
  172. int size;
  173. int retval;
  174. switch (type) {
  175. case ACL_TYPE_ACCESS:
  176. name = POSIX_ACL_XATTR_ACCESS;
  177. break;
  178. case ACL_TYPE_DEFAULT:
  179. name = POSIX_ACL_XATTR_DEFAULT;
  180. break;
  181. default:
  182. BUG();
  183. }
  184. size = reiserfs_xattr_get(inode, name, NULL, 0);
  185. if (size < 0) {
  186. if (size == -ENODATA || size == -ENOSYS) {
  187. set_cached_acl(inode, type, NULL);
  188. return NULL;
  189. }
  190. return ERR_PTR(size);
  191. }
  192. value = kmalloc(size, GFP_NOFS);
  193. if (!value)
  194. return ERR_PTR(-ENOMEM);
  195. retval = reiserfs_xattr_get(inode, name, value, size);
  196. if (retval == -ENODATA || retval == -ENOSYS) {
  197. /*
  198. * This shouldn't actually happen as it should have
  199. * been caught above.. but just in case
  200. */
  201. acl = NULL;
  202. } else if (retval < 0) {
  203. acl = ERR_PTR(retval);
  204. } else {
  205. acl = reiserfs_posix_acl_from_disk(value, retval);
  206. }
  207. if (!IS_ERR(acl))
  208. set_cached_acl(inode, type, acl);
  209. kfree(value);
  210. return acl;
  211. }
  212. /*
  213. * Inode operation set_posix_acl().
  214. *
  215. * inode->i_mutex: down
  216. * BKL held [before 2.5.x]
  217. */
  218. static int
  219. __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
  220. int type, struct posix_acl *acl)
  221. {
  222. char *name;
  223. void *value = NULL;
  224. size_t size = 0;
  225. int error;
  226. switch (type) {
  227. case ACL_TYPE_ACCESS:
  228. name = POSIX_ACL_XATTR_ACCESS;
  229. break;
  230. case ACL_TYPE_DEFAULT:
  231. name = POSIX_ACL_XATTR_DEFAULT;
  232. if (!S_ISDIR(inode->i_mode))
  233. return acl ? -EACCES : 0;
  234. break;
  235. default:
  236. return -EINVAL;
  237. }
  238. if (acl) {
  239. value = reiserfs_posix_acl_to_disk(acl, &size);
  240. if (IS_ERR(value))
  241. return (int)PTR_ERR(value);
  242. }
  243. error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
  244. /*
  245. * Ensure that the inode gets dirtied if we're only using
  246. * the mode bits and an old ACL didn't exist. We don't need
  247. * to check if the inode is hashed here since we won't get
  248. * called by reiserfs_inherit_default_acl().
  249. */
  250. if (error == -ENODATA) {
  251. error = 0;
  252. if (type == ACL_TYPE_ACCESS) {
  253. inode->i_ctime = CURRENT_TIME_SEC;
  254. mark_inode_dirty(inode);
  255. }
  256. }
  257. kfree(value);
  258. if (!error)
  259. set_cached_acl(inode, type, acl);
  260. return error;
  261. }
  262. /*
  263. * dir->i_mutex: locked,
  264. * inode is new and not released into the wild yet
  265. */
  266. int
  267. reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
  268. struct inode *dir, struct dentry *dentry,
  269. struct inode *inode)
  270. {
  271. struct posix_acl *default_acl, *acl;
  272. int err = 0;
  273. /* ACLs only get applied to files and directories */
  274. if (S_ISLNK(inode->i_mode))
  275. return 0;
  276. /*
  277. * ACLs can only be used on "new" objects, so if it's an old object
  278. * there is nothing to inherit from
  279. */
  280. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  281. goto apply_umask;
  282. /*
  283. * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  284. * would be useless since permissions are ignored, and a pain because
  285. * it introduces locking cycles
  286. */
  287. if (IS_PRIVATE(dir)) {
  288. inode->i_flags |= S_PRIVATE;
  289. goto apply_umask;
  290. }
  291. err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  292. if (err)
  293. return err;
  294. if (default_acl) {
  295. err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
  296. default_acl);
  297. posix_acl_release(default_acl);
  298. }
  299. if (acl) {
  300. if (!err)
  301. err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
  302. acl);
  303. posix_acl_release(acl);
  304. }
  305. return err;
  306. apply_umask:
  307. /* no ACL, apply umask */
  308. inode->i_mode &= ~current_umask();
  309. return err;
  310. }
  311. /* This is used to cache the default acl before a new object is created.
  312. * The biggest reason for this is to get an idea of how many blocks will
  313. * actually be required for the create operation if we must inherit an ACL.
  314. * An ACL write can add up to 3 object creations and an additional file write
  315. * so we'd prefer not to reserve that many blocks in the journal if we can.
  316. * It also has the advantage of not loading the ACL with a transaction open,
  317. * this may seem silly, but if the owner of the directory is doing the
  318. * creation, the ACL may not be loaded since the permissions wouldn't require
  319. * it.
  320. * We return the number of blocks required for the transaction.
  321. */
  322. int reiserfs_cache_default_acl(struct inode *inode)
  323. {
  324. struct posix_acl *acl;
  325. int nblocks = 0;
  326. if (IS_PRIVATE(inode))
  327. return 0;
  328. acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
  329. if (acl && !IS_ERR(acl)) {
  330. int size = reiserfs_acl_size(acl->a_count);
  331. /* Other xattrs can be created during inode creation. We don't
  332. * want to claim too many blocks, so we check to see if we
  333. * we need to create the tree to the xattrs, and then we
  334. * just want two files. */
  335. nblocks = reiserfs_xattr_jcreate_nblocks(inode);
  336. nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
  337. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  338. /* We need to account for writes + bitmaps for two files */
  339. nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
  340. posix_acl_release(acl);
  341. }
  342. return nblocks;
  343. }
  344. /*
  345. * Called under i_mutex
  346. */
  347. int reiserfs_acl_chmod(struct inode *inode)
  348. {
  349. if (IS_PRIVATE(inode))
  350. return 0;
  351. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  352. !reiserfs_posixacl(inode->i_sb))
  353. return 0;
  354. return posix_acl_chmod(inode, inode->i_mode);
  355. }