xattr_acl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 = XATTR_NAME_POSIX_ACL_ACCESS;
  177. break;
  178. case ACL_TYPE_DEFAULT:
  179. name = XATTR_NAME_POSIX_ACL_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. return NULL;
  188. return ERR_PTR(size);
  189. }
  190. value = kmalloc(size, GFP_NOFS);
  191. if (!value)
  192. return ERR_PTR(-ENOMEM);
  193. retval = reiserfs_xattr_get(inode, name, value, size);
  194. if (retval == -ENODATA || retval == -ENOSYS) {
  195. /*
  196. * This shouldn't actually happen as it should have
  197. * been caught above.. but just in case
  198. */
  199. acl = NULL;
  200. } else if (retval < 0) {
  201. acl = ERR_PTR(retval);
  202. } else {
  203. acl = reiserfs_posix_acl_from_disk(value, retval);
  204. }
  205. kfree(value);
  206. return acl;
  207. }
  208. /*
  209. * Inode operation set_posix_acl().
  210. *
  211. * inode->i_mutex: down
  212. * BKL held [before 2.5.x]
  213. */
  214. static int
  215. __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
  216. int type, struct posix_acl *acl)
  217. {
  218. char *name;
  219. void *value = NULL;
  220. size_t size = 0;
  221. int error;
  222. switch (type) {
  223. case ACL_TYPE_ACCESS:
  224. name = XATTR_NAME_POSIX_ACL_ACCESS;
  225. break;
  226. case ACL_TYPE_DEFAULT:
  227. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  228. if (!S_ISDIR(inode->i_mode))
  229. return acl ? -EACCES : 0;
  230. break;
  231. default:
  232. return -EINVAL;
  233. }
  234. if (acl) {
  235. value = reiserfs_posix_acl_to_disk(acl, &size);
  236. if (IS_ERR(value))
  237. return (int)PTR_ERR(value);
  238. }
  239. error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
  240. /*
  241. * Ensure that the inode gets dirtied if we're only using
  242. * the mode bits and an old ACL didn't exist. We don't need
  243. * to check if the inode is hashed here since we won't get
  244. * called by reiserfs_inherit_default_acl().
  245. */
  246. if (error == -ENODATA) {
  247. error = 0;
  248. if (type == ACL_TYPE_ACCESS) {
  249. inode->i_ctime = current_time(inode);
  250. mark_inode_dirty(inode);
  251. }
  252. }
  253. kfree(value);
  254. if (!error)
  255. set_cached_acl(inode, type, acl);
  256. return error;
  257. }
  258. /*
  259. * dir->i_mutex: locked,
  260. * inode is new and not released into the wild yet
  261. */
  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. /*
  273. * ACLs can only be used on "new" objects, so if it's an old object
  274. * there is nothing to inherit from
  275. */
  276. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  277. goto apply_umask;
  278. /*
  279. * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  280. * would be useless since permissions are ignored, and a pain because
  281. * it introduces locking cycles
  282. */
  283. if (IS_PRIVATE(dir)) {
  284. inode->i_flags |= S_PRIVATE;
  285. goto apply_umask;
  286. }
  287. err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  288. if (err)
  289. return err;
  290. if (default_acl) {
  291. err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
  292. default_acl);
  293. posix_acl_release(default_acl);
  294. }
  295. if (acl) {
  296. if (!err)
  297. err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
  298. acl);
  299. posix_acl_release(acl);
  300. }
  301. return err;
  302. apply_umask:
  303. /* no ACL, apply umask */
  304. inode->i_mode &= ~current_umask();
  305. return err;
  306. }
  307. /* This is used to cache the default acl before a new object is created.
  308. * The biggest reason for this is to get an idea of how many blocks will
  309. * actually be required for the create operation if we must inherit an ACL.
  310. * An ACL write can add up to 3 object creations and an additional file write
  311. * so we'd prefer not to reserve that many blocks in the journal if we can.
  312. * It also has the advantage of not loading the ACL with a transaction open,
  313. * this may seem silly, but if the owner of the directory is doing the
  314. * creation, the ACL may not be loaded since the permissions wouldn't require
  315. * it.
  316. * We return the number of blocks required for the transaction.
  317. */
  318. int reiserfs_cache_default_acl(struct inode *inode)
  319. {
  320. struct posix_acl *acl;
  321. int nblocks = 0;
  322. if (IS_PRIVATE(inode))
  323. return 0;
  324. acl = get_acl(inode, ACL_TYPE_DEFAULT);
  325. if (acl && !IS_ERR(acl)) {
  326. int size = reiserfs_acl_size(acl->a_count);
  327. /* Other xattrs can be created during inode creation. We don't
  328. * want to claim too many blocks, so we check to see if we
  329. * we need to create the tree to the xattrs, and then we
  330. * just want two files. */
  331. nblocks = reiserfs_xattr_jcreate_nblocks(inode);
  332. nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
  333. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  334. /* We need to account for writes + bitmaps for two files */
  335. nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
  336. posix_acl_release(acl);
  337. }
  338. return nblocks;
  339. }
  340. /*
  341. * Called under i_mutex
  342. */
  343. int reiserfs_acl_chmod(struct inode *inode)
  344. {
  345. if (IS_PRIVATE(inode))
  346. return 0;
  347. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  348. !reiserfs_posixacl(inode->i_sb))
  349. return 0;
  350. return posix_acl_chmod(inode, inode->i_mode);
  351. }