xattr_acl.c 9.6 KB

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