xattr_acl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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_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 = XATTR_NAME_POSIX_ACL_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. /*
  261. * dir->i_mutex: locked,
  262. * inode is new and not released into the wild yet
  263. */
  264. int
  265. reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
  266. struct inode *dir, struct dentry *dentry,
  267. struct inode *inode)
  268. {
  269. struct posix_acl *default_acl, *acl;
  270. int err = 0;
  271. /* ACLs only get applied to files and directories */
  272. if (S_ISLNK(inode->i_mode))
  273. return 0;
  274. /*
  275. * ACLs can only be used on "new" objects, so if it's an old object
  276. * there is nothing to inherit from
  277. */
  278. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  279. goto apply_umask;
  280. /*
  281. * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  282. * would be useless since permissions are ignored, and a pain because
  283. * it introduces locking cycles
  284. */
  285. if (IS_PRIVATE(dir)) {
  286. inode->i_flags |= S_PRIVATE;
  287. goto apply_umask;
  288. }
  289. err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  290. if (err)
  291. return err;
  292. if (default_acl) {
  293. err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
  294. default_acl);
  295. posix_acl_release(default_acl);
  296. }
  297. if (acl) {
  298. if (!err)
  299. err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
  300. acl);
  301. posix_acl_release(acl);
  302. }
  303. return err;
  304. apply_umask:
  305. /* no ACL, apply umask */
  306. inode->i_mode &= ~current_umask();
  307. return err;
  308. }
  309. /* This is used to cache the default acl before a new object is created.
  310. * The biggest reason for this is to get an idea of how many blocks will
  311. * actually be required for the create operation if we must inherit an ACL.
  312. * An ACL write can add up to 3 object creations and an additional file write
  313. * so we'd prefer not to reserve that many blocks in the journal if we can.
  314. * It also has the advantage of not loading the ACL with a transaction open,
  315. * this may seem silly, but if the owner of the directory is doing the
  316. * creation, the ACL may not be loaded since the permissions wouldn't require
  317. * it.
  318. * We return the number of blocks required for the transaction.
  319. */
  320. int reiserfs_cache_default_acl(struct inode *inode)
  321. {
  322. struct posix_acl *acl;
  323. int nblocks = 0;
  324. if (IS_PRIVATE(inode))
  325. return 0;
  326. acl = get_acl(inode, ACL_TYPE_DEFAULT);
  327. if (acl && !IS_ERR(acl)) {
  328. int size = reiserfs_acl_size(acl->a_count);
  329. /* Other xattrs can be created during inode creation. We don't
  330. * want to claim too many blocks, so we check to see if we
  331. * we need to create the tree to the xattrs, and then we
  332. * just want two files. */
  333. nblocks = reiserfs_xattr_jcreate_nblocks(inode);
  334. nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
  335. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  336. /* We need to account for writes + bitmaps for two files */
  337. nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
  338. posix_acl_release(acl);
  339. }
  340. return nblocks;
  341. }
  342. /*
  343. * Called under i_mutex
  344. */
  345. int reiserfs_acl_chmod(struct inode *inode)
  346. {
  347. if (IS_PRIVATE(inode))
  348. return 0;
  349. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  350. !reiserfs_posixacl(inode->i_sb))
  351. return 0;
  352. return posix_acl_chmod(inode, inode->i_mode);
  353. }