xattr_acl.c 9.5 KB

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