xattr_acl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  245. if (error < 0)
  246. return error;
  247. else {
  248. if (error == 0)
  249. acl = NULL;
  250. }
  251. }
  252. break;
  253. case ACL_TYPE_DEFAULT:
  254. name = POSIX_ACL_XATTR_DEFAULT;
  255. if (!S_ISDIR(inode->i_mode))
  256. return acl ? -EACCES : 0;
  257. break;
  258. default:
  259. return -EINVAL;
  260. }
  261. if (acl) {
  262. value = posix_acl_to_disk(acl, &size);
  263. if (IS_ERR(value))
  264. return (int)PTR_ERR(value);
  265. }
  266. error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
  267. /*
  268. * Ensure that the inode gets dirtied if we're only using
  269. * the mode bits and an old ACL didn't exist. We don't need
  270. * to check if the inode is hashed here since we won't get
  271. * called by reiserfs_inherit_default_acl().
  272. */
  273. if (error == -ENODATA) {
  274. error = 0;
  275. if (type == ACL_TYPE_ACCESS) {
  276. inode->i_ctime = CURRENT_TIME_SEC;
  277. mark_inode_dirty(inode);
  278. }
  279. }
  280. kfree(value);
  281. if (!error)
  282. set_cached_acl(inode, type, acl);
  283. return error;
  284. }
  285. /* dir->i_mutex: locked,
  286. * inode is new and not released into the wild yet */
  287. int
  288. reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
  289. struct inode *dir, struct dentry *dentry,
  290. struct inode *inode)
  291. {
  292. struct posix_acl *acl;
  293. int err = 0;
  294. /* ACLs only get applied to files and directories */
  295. if (S_ISLNK(inode->i_mode))
  296. return 0;
  297. /* ACLs can only be used on "new" objects, so if it's an old object
  298. * there is nothing to inherit from */
  299. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  300. goto apply_umask;
  301. /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  302. * would be useless since permissions are ignored, and a pain because
  303. * it introduces locking cycles */
  304. if (IS_PRIVATE(dir)) {
  305. inode->i_flags |= S_PRIVATE;
  306. goto apply_umask;
  307. }
  308. acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
  309. if (IS_ERR(acl))
  310. return PTR_ERR(acl);
  311. if (acl) {
  312. /* Copy the default ACL to the default ACL of a new directory */
  313. if (S_ISDIR(inode->i_mode)) {
  314. err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
  315. acl);
  316. if (err)
  317. goto cleanup;
  318. }
  319. /* Now we reconcile the new ACL and the mode,
  320. potentially modifying both */
  321. err = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
  322. if (err < 0)
  323. return err;
  324. /* If we need an ACL.. */
  325. if (err > 0)
  326. err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl);
  327. cleanup:
  328. posix_acl_release(acl);
  329. } else {
  330. apply_umask:
  331. /* no ACL, apply umask */
  332. inode->i_mode &= ~current_umask();
  333. }
  334. return err;
  335. }
  336. /* This is used to cache the default acl before a new object is created.
  337. * The biggest reason for this is to get an idea of how many blocks will
  338. * actually be required for the create operation if we must inherit an ACL.
  339. * An ACL write can add up to 3 object creations and an additional file write
  340. * so we'd prefer not to reserve that many blocks in the journal if we can.
  341. * It also has the advantage of not loading the ACL with a transaction open,
  342. * this may seem silly, but if the owner of the directory is doing the
  343. * creation, the ACL may not be loaded since the permissions wouldn't require
  344. * it.
  345. * We return the number of blocks required for the transaction.
  346. */
  347. int reiserfs_cache_default_acl(struct inode *inode)
  348. {
  349. struct posix_acl *acl;
  350. int nblocks = 0;
  351. if (IS_PRIVATE(inode))
  352. return 0;
  353. acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
  354. if (acl && !IS_ERR(acl)) {
  355. int size = reiserfs_acl_size(acl->a_count);
  356. /* Other xattrs can be created during inode creation. We don't
  357. * want to claim too many blocks, so we check to see if we
  358. * we need to create the tree to the xattrs, and then we
  359. * just want two files. */
  360. nblocks = reiserfs_xattr_jcreate_nblocks(inode);
  361. nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
  362. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  363. /* We need to account for writes + bitmaps for two files */
  364. nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
  365. posix_acl_release(acl);
  366. }
  367. return nblocks;
  368. }
  369. int reiserfs_acl_chmod(struct inode *inode)
  370. {
  371. struct reiserfs_transaction_handle th;
  372. struct posix_acl *acl;
  373. size_t size;
  374. int depth;
  375. int error;
  376. if (S_ISLNK(inode->i_mode))
  377. return -EOPNOTSUPP;
  378. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  379. !reiserfs_posixacl(inode->i_sb)) {
  380. return 0;
  381. }
  382. reiserfs_write_unlock(inode->i_sb);
  383. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  384. reiserfs_write_lock(inode->i_sb);
  385. if (!acl)
  386. return 0;
  387. if (IS_ERR(acl))
  388. return PTR_ERR(acl);
  389. error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode);
  390. if (error)
  391. return error;
  392. size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count));
  393. depth = reiserfs_write_lock_once(inode->i_sb);
  394. error = journal_begin(&th, inode->i_sb, size * 2);
  395. if (!error) {
  396. int error2;
  397. error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl);
  398. error2 = journal_end(&th, inode->i_sb, size * 2);
  399. if (error2)
  400. error = error2;
  401. }
  402. reiserfs_write_unlock_once(inode->i_sb, depth);
  403. posix_acl_release(acl);
  404. return error;
  405. }
  406. static size_t posix_acl_access_list(struct dentry *dentry, char *list,
  407. size_t list_size, const char *name,
  408. size_t name_len, int type)
  409. {
  410. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  411. if (!reiserfs_posixacl(dentry->d_sb))
  412. return 0;
  413. if (list && size <= list_size)
  414. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  415. return size;
  416. }
  417. const struct xattr_handler reiserfs_posix_acl_access_handler = {
  418. .prefix = POSIX_ACL_XATTR_ACCESS,
  419. .flags = ACL_TYPE_ACCESS,
  420. .get = posix_acl_get,
  421. .set = posix_acl_set,
  422. .list = posix_acl_access_list,
  423. };
  424. static size_t posix_acl_default_list(struct dentry *dentry, char *list,
  425. size_t list_size, const char *name,
  426. size_t name_len, int type)
  427. {
  428. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  429. if (!reiserfs_posixacl(dentry->d_sb))
  430. return 0;
  431. if (list && size <= list_size)
  432. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  433. return size;
  434. }
  435. const struct xattr_handler reiserfs_posix_acl_default_handler = {
  436. .prefix = POSIX_ACL_XATTR_DEFAULT,
  437. .flags = ACL_TYPE_DEFAULT,
  438. .get = posix_acl_get,
  439. .set = posix_acl_set,
  440. .list = posix_acl_default_list,
  441. };