xattr_acl.c 13 KB

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