xattr_acl.c 9.4 KB

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