PageRenderTime 24ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/reiserfs/xattr_acl.c

https://bitbucket.org/zozo123/boeffla-kernel-jb-u6-s3
C | 531 lines | 409 code | 65 blank | 57 comment | 95 complexity | 027688568261e3e30f4c6603397d266a MD5 | raw file
  1. #include <linux/capability.h>
  2. #include <linux/fs.h>
  3. #include <linux/posix_acl.h>
  4. #include <linux/reiserfs_fs.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 <linux/reiserfs_xattr.h>
  11. #include <linux/reiserfs_acl.h>
  12. #include <asm/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. static int
  17. posix_acl_set(struct dentry *dentry, const char *name, const void *value,
  18. size_t size, int flags, int type)
  19. {
  20. struct inode *inode = dentry->d_inode;
  21. struct posix_acl *acl;
  22. int error, error2;
  23. struct reiserfs_transaction_handle th;
  24. size_t jcreate_blocks;
  25. if (!reiserfs_posixacl(inode->i_sb))
  26. return -EOPNOTSUPP;
  27. if (!inode_owner_or_capable(inode))
  28. return -EPERM;
  29. if (value) {
  30. acl = posix_acl_from_xattr(value, size);
  31. if (IS_ERR(acl)) {
  32. return PTR_ERR(acl);
  33. } else if (acl) {
  34. error = posix_acl_valid(acl);
  35. if (error)
  36. goto release_and_out;
  37. }
  38. } else
  39. acl = NULL;
  40. /* Pessimism: We can't assume that anything from the xattr root up
  41. * has been created. */
  42. jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
  43. reiserfs_xattr_nblocks(inode, size) * 2;
  44. reiserfs_write_lock(inode->i_sb);
  45. error = journal_begin(&th, inode->i_sb, jcreate_blocks);
  46. if (error == 0) {
  47. error = reiserfs_set_acl(&th, inode, type, acl);
  48. error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
  49. if (error2)
  50. error = error2;
  51. }
  52. reiserfs_write_unlock(inode->i_sb);
  53. release_and_out:
  54. posix_acl_release(acl);
  55. return error;
  56. }
  57. static int
  58. posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
  59. size_t size, int type)
  60. {
  61. struct posix_acl *acl;
  62. int error;
  63. if (!reiserfs_posixacl(dentry->d_sb))
  64. return -EOPNOTSUPP;
  65. acl = reiserfs_get_acl(dentry->d_inode, type);
  66. if (IS_ERR(acl))
  67. return PTR_ERR(acl);
  68. if (acl == NULL)
  69. return -ENODATA;
  70. error = posix_acl_to_xattr(acl, buffer, size);
  71. posix_acl_release(acl);
  72. return error;
  73. }
  74. /*
  75. * Convert from filesystem to in-memory representation.
  76. */
  77. static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
  78. {
  79. const char *end = (char *)value + size;
  80. int n, count;
  81. struct posix_acl *acl;
  82. if (!value)
  83. return NULL;
  84. if (size < sizeof(reiserfs_acl_header))
  85. return ERR_PTR(-EINVAL);
  86. if (((reiserfs_acl_header *) value)->a_version !=
  87. cpu_to_le32(REISERFS_ACL_VERSION))
  88. return ERR_PTR(-EINVAL);
  89. value = (char *)value + sizeof(reiserfs_acl_header);
  90. count = reiserfs_acl_count(size);
  91. if (count < 0)
  92. return ERR_PTR(-EINVAL);
  93. if (count == 0)
  94. return NULL;
  95. acl = posix_acl_alloc(count, GFP_NOFS);
  96. if (!acl)
  97. return ERR_PTR(-ENOMEM);
  98. for (n = 0; n < count; n++) {
  99. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
  100. if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
  101. goto fail;
  102. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  103. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  104. switch (acl->a_entries[n].e_tag) {
  105. case ACL_USER_OBJ:
  106. case ACL_GROUP_OBJ:
  107. case ACL_MASK:
  108. case ACL_OTHER:
  109. value = (char *)value +
  110. sizeof(reiserfs_acl_entry_short);
  111. acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
  112. break;
  113. case ACL_USER:
  114. case ACL_GROUP:
  115. value = (char *)value + sizeof(reiserfs_acl_entry);
  116. if ((char *)value > end)
  117. goto fail;
  118. acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
  119. break;
  120. default:
  121. goto fail;
  122. }
  123. }
  124. if (value != end)
  125. goto fail;
  126. return acl;
  127. fail:
  128. posix_acl_release(acl);
  129. return ERR_PTR(-EINVAL);
  130. }
  131. /*
  132. * Convert from in-memory to filesystem representation.
  133. */
  134. static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
  135. {
  136. reiserfs_acl_header *ext_acl;
  137. char *e;
  138. int n;
  139. *size = reiserfs_acl_size(acl->a_count);
  140. ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
  141. acl->a_count *
  142. sizeof(reiserfs_acl_entry),
  143. GFP_NOFS);
  144. if (!ext_acl)
  145. return ERR_PTR(-ENOMEM);
  146. ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
  147. e = (char *)ext_acl + sizeof(reiserfs_acl_header);
  148. for (n = 0; n < acl->a_count; n++) {
  149. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
  150. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  151. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  152. switch (acl->a_entries[n].e_tag) {
  153. case ACL_USER:
  154. case ACL_GROUP:
  155. entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
  156. e += sizeof(reiserfs_acl_entry);
  157. break;
  158. case ACL_USER_OBJ:
  159. case ACL_GROUP_OBJ:
  160. case ACL_MASK:
  161. case ACL_OTHER:
  162. e += sizeof(reiserfs_acl_entry_short);
  163. break;
  164. default:
  165. goto fail;
  166. }
  167. }
  168. return (char *)ext_acl;
  169. fail:
  170. kfree(ext_acl);
  171. return ERR_PTR(-EINVAL);
  172. }
  173. /*
  174. * Inode operation get_posix_acl().
  175. *
  176. * inode->i_mutex: down
  177. * BKL held [before 2.5.x]
  178. */
  179. struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
  180. {
  181. char *name, *value;
  182. struct posix_acl *acl;
  183. int size;
  184. int retval;
  185. acl = get_cached_acl(inode, type);
  186. if (acl != ACL_NOT_CACHED)
  187. return acl;
  188. switch (type) {
  189. case ACL_TYPE_ACCESS:
  190. name = POSIX_ACL_XATTR_ACCESS;
  191. break;
  192. case ACL_TYPE_DEFAULT:
  193. name = POSIX_ACL_XATTR_DEFAULT;
  194. break;
  195. default:
  196. BUG();
  197. }
  198. size = reiserfs_xattr_get(inode, name, NULL, 0);
  199. if (size < 0) {
  200. if (size == -ENODATA || size == -ENOSYS) {
  201. set_cached_acl(inode, type, NULL);
  202. return NULL;
  203. }
  204. return ERR_PTR(size);
  205. }
  206. value = kmalloc(size, GFP_NOFS);
  207. if (!value)
  208. return ERR_PTR(-ENOMEM);
  209. retval = reiserfs_xattr_get(inode, name, value, size);
  210. if (retval == -ENODATA || retval == -ENOSYS) {
  211. /* This shouldn't actually happen as it should have
  212. been caught above.. but just in case */
  213. acl = NULL;
  214. } else if (retval < 0) {
  215. acl = ERR_PTR(retval);
  216. } else {
  217. acl = posix_acl_from_disk(value, retval);
  218. }
  219. if (!IS_ERR(acl))
  220. set_cached_acl(inode, type, acl);
  221. kfree(value);
  222. return acl;
  223. }
  224. /*
  225. * Inode operation set_posix_acl().
  226. *
  227. * inode->i_mutex: down
  228. * BKL held [before 2.5.x]
  229. */
  230. static int
  231. reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
  232. int type, struct posix_acl *acl)
  233. {
  234. char *name;
  235. void *value = NULL;
  236. size_t size = 0;
  237. int error;
  238. if (S_ISLNK(inode->i_mode))
  239. return -EOPNOTSUPP;
  240. switch (type) {
  241. case ACL_TYPE_ACCESS:
  242. name = POSIX_ACL_XATTR_ACCESS;
  243. if (acl) {
  244. mode_t mode = inode->i_mode;
  245. error = posix_acl_equiv_mode(acl, &mode);
  246. if (error < 0)
  247. return error;
  248. else {
  249. inode->i_mode = mode;
  250. if (error == 0)
  251. acl = NULL;
  252. }
  253. }
  254. break;
  255. case ACL_TYPE_DEFAULT:
  256. name = POSIX_ACL_XATTR_DEFAULT;
  257. if (!S_ISDIR(inode->i_mode))
  258. return acl ? -EACCES : 0;
  259. break;
  260. default:
  261. return -EINVAL;
  262. }
  263. if (acl) {
  264. value = posix_acl_to_disk(acl, &size);
  265. if (IS_ERR(value))
  266. return (int)PTR_ERR(value);
  267. }
  268. error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
  269. /*
  270. * Ensure that the inode gets dirtied if we're only using
  271. * the mode bits and an old ACL didn't exist. We don't need
  272. * to check if the inode is hashed here since we won't get
  273. * called by reiserfs_inherit_default_acl().
  274. */
  275. if (error == -ENODATA) {
  276. error = 0;
  277. if (type == ACL_TYPE_ACCESS) {
  278. inode->i_ctime = CURRENT_TIME_SEC;
  279. mark_inode_dirty(inode);
  280. }
  281. }
  282. kfree(value);
  283. if (!error)
  284. set_cached_acl(inode, type, acl);
  285. return error;
  286. }
  287. /* dir->i_mutex: locked,
  288. * inode is new and not released into the wild yet */
  289. int
  290. reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
  291. struct inode *dir, struct dentry *dentry,
  292. struct inode *inode)
  293. {
  294. struct posix_acl *acl;
  295. int err = 0;
  296. /* ACLs only get applied to files and directories */
  297. if (S_ISLNK(inode->i_mode))
  298. return 0;
  299. /* ACLs can only be used on "new" objects, so if it's an old object
  300. * there is nothing to inherit from */
  301. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  302. goto apply_umask;
  303. /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  304. * would be useless since permissions are ignored, and a pain because
  305. * it introduces locking cycles */
  306. if (IS_PRIVATE(dir)) {
  307. inode->i_flags |= S_PRIVATE;
  308. goto apply_umask;
  309. }
  310. acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
  311. if (IS_ERR(acl))
  312. return PTR_ERR(acl);
  313. if (acl) {
  314. struct posix_acl *acl_copy;
  315. mode_t mode = inode->i_mode;
  316. int need_acl;
  317. /* Copy the default ACL to the default ACL of a new directory */
  318. if (S_ISDIR(inode->i_mode)) {
  319. err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
  320. acl);
  321. if (err)
  322. goto cleanup;
  323. }
  324. /* Now we reconcile the new ACL and the mode,
  325. potentially modifying both */
  326. acl_copy = posix_acl_clone(acl, GFP_NOFS);
  327. if (!acl_copy) {
  328. err = -ENOMEM;
  329. goto cleanup;
  330. }
  331. need_acl = posix_acl_create_masq(acl_copy, &mode);
  332. if (need_acl >= 0) {
  333. if (mode != inode->i_mode) {
  334. inode->i_mode = mode;
  335. }
  336. /* If we need an ACL.. */
  337. if (need_acl > 0) {
  338. err = reiserfs_set_acl(th, inode,
  339. ACL_TYPE_ACCESS,
  340. acl_copy);
  341. if (err)
  342. goto cleanup_copy;
  343. }
  344. }
  345. cleanup_copy:
  346. posix_acl_release(acl_copy);
  347. cleanup:
  348. posix_acl_release(acl);
  349. } else {
  350. apply_umask:
  351. /* no ACL, apply umask */
  352. inode->i_mode &= ~current_umask();
  353. }
  354. return err;
  355. }
  356. /* This is used to cache the default acl before a new object is created.
  357. * The biggest reason for this is to get an idea of how many blocks will
  358. * actually be required for the create operation if we must inherit an ACL.
  359. * An ACL write can add up to 3 object creations and an additional file write
  360. * so we'd prefer not to reserve that many blocks in the journal if we can.
  361. * It also has the advantage of not loading the ACL with a transaction open,
  362. * this may seem silly, but if the owner of the directory is doing the
  363. * creation, the ACL may not be loaded since the permissions wouldn't require
  364. * it.
  365. * We return the number of blocks required for the transaction.
  366. */
  367. int reiserfs_cache_default_acl(struct inode *inode)
  368. {
  369. struct posix_acl *acl;
  370. int nblocks = 0;
  371. if (IS_PRIVATE(inode))
  372. return 0;
  373. acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
  374. if (acl && !IS_ERR(acl)) {
  375. int size = reiserfs_acl_size(acl->a_count);
  376. /* Other xattrs can be created during inode creation. We don't
  377. * want to claim too many blocks, so we check to see if we
  378. * we need to create the tree to the xattrs, and then we
  379. * just want two files. */
  380. nblocks = reiserfs_xattr_jcreate_nblocks(inode);
  381. nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
  382. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  383. /* We need to account for writes + bitmaps for two files */
  384. nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
  385. posix_acl_release(acl);
  386. }
  387. return nblocks;
  388. }
  389. int reiserfs_acl_chmod(struct inode *inode)
  390. {
  391. struct posix_acl *acl, *clone;
  392. int error;
  393. if (S_ISLNK(inode->i_mode))
  394. return -EOPNOTSUPP;
  395. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  396. !reiserfs_posixacl(inode->i_sb)) {
  397. return 0;
  398. }
  399. reiserfs_write_unlock(inode->i_sb);
  400. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  401. reiserfs_write_lock(inode->i_sb);
  402. if (!acl)
  403. return 0;
  404. if (IS_ERR(acl))
  405. return PTR_ERR(acl);
  406. clone = posix_acl_clone(acl, GFP_NOFS);
  407. posix_acl_release(acl);
  408. if (!clone)
  409. return -ENOMEM;
  410. error = posix_acl_chmod_masq(clone, inode->i_mode);
  411. if (!error) {
  412. struct reiserfs_transaction_handle th;
  413. size_t size = reiserfs_xattr_nblocks(inode,
  414. reiserfs_acl_size(clone->a_count));
  415. int depth;
  416. depth = reiserfs_write_lock_once(inode->i_sb);
  417. error = journal_begin(&th, inode->i_sb, size * 2);
  418. if (!error) {
  419. int error2;
  420. error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS,
  421. clone);
  422. error2 = journal_end(&th, inode->i_sb, size * 2);
  423. if (error2)
  424. error = error2;
  425. }
  426. reiserfs_write_unlock_once(inode->i_sb, depth);
  427. }
  428. posix_acl_release(clone);
  429. return error;
  430. }
  431. static size_t posix_acl_access_list(struct dentry *dentry, char *list,
  432. size_t list_size, const char *name,
  433. size_t name_len, int type)
  434. {
  435. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  436. if (!reiserfs_posixacl(dentry->d_sb))
  437. return 0;
  438. if (list && size <= list_size)
  439. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  440. return size;
  441. }
  442. const struct xattr_handler reiserfs_posix_acl_access_handler = {
  443. .prefix = POSIX_ACL_XATTR_ACCESS,
  444. .flags = ACL_TYPE_ACCESS,
  445. .get = posix_acl_get,
  446. .set = posix_acl_set,
  447. .list = posix_acl_access_list,
  448. };
  449. static size_t posix_acl_default_list(struct dentry *dentry, char *list,
  450. size_t list_size, const char *name,
  451. size_t name_len, int type)
  452. {
  453. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  454. if (!reiserfs_posixacl(dentry->d_sb))
  455. return 0;
  456. if (list && size <= list_size)
  457. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  458. return size;
  459. }
  460. const struct xattr_handler reiserfs_posix_acl_default_handler = {
  461. .prefix = POSIX_ACL_XATTR_DEFAULT,
  462. .flags = ACL_TYPE_DEFAULT,
  463. .get = posix_acl_get,
  464. .set = posix_acl_set,
  465. .list = posix_acl_default_list,
  466. };