xattr_acl.c 9.6 KB

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