xattr_acl.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * linux/fs/xattr_acl.c
  3. *
  4. * Almost all from linux/fs/ext2/acl.c:
  5. * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/slab.h>
  10. #include <linux/fs.h>
  11. #include <linux/posix_acl_xattr.h>
  12. /*
  13. * Convert from extended attribute to in-memory representation.
  14. */
  15. struct posix_acl *
  16. posix_acl_from_xattr(const void *value, size_t size)
  17. {
  18. posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
  19. posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
  20. int count;
  21. struct posix_acl *acl;
  22. struct posix_acl_entry *acl_e;
  23. if (!value)
  24. return NULL;
  25. if (size < sizeof(posix_acl_xattr_header))
  26. return ERR_PTR(-EINVAL);
  27. if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  28. return ERR_PTR(-EOPNOTSUPP);
  29. count = posix_acl_xattr_count(size);
  30. if (count < 0)
  31. return ERR_PTR(-EINVAL);
  32. if (count == 0)
  33. return NULL;
  34. acl = posix_acl_alloc(count, GFP_KERNEL);
  35. if (!acl)
  36. return ERR_PTR(-ENOMEM);
  37. acl_e = acl->a_entries;
  38. for (end = entry + count; entry != end; acl_e++, entry++) {
  39. acl_e->e_tag = le16_to_cpu(entry->e_tag);
  40. acl_e->e_perm = le16_to_cpu(entry->e_perm);
  41. switch(acl_e->e_tag) {
  42. case ACL_USER_OBJ:
  43. case ACL_GROUP_OBJ:
  44. case ACL_MASK:
  45. case ACL_OTHER:
  46. acl_e->e_id = ACL_UNDEFINED_ID;
  47. break;
  48. case ACL_USER:
  49. case ACL_GROUP:
  50. acl_e->e_id = le32_to_cpu(entry->e_id);
  51. break;
  52. default:
  53. goto fail;
  54. }
  55. }
  56. return acl;
  57. fail:
  58. posix_acl_release(acl);
  59. return ERR_PTR(-EINVAL);
  60. }
  61. EXPORT_SYMBOL (posix_acl_from_xattr);
  62. /*
  63. * Convert from in-memory to extended attribute representation.
  64. */
  65. int
  66. posix_acl_to_xattr(const struct posix_acl *acl, void *buffer, size_t size)
  67. {
  68. posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer;
  69. posix_acl_xattr_entry *ext_entry = ext_acl->a_entries;
  70. int real_size, n;
  71. real_size = posix_acl_xattr_size(acl->a_count);
  72. if (!buffer)
  73. return real_size;
  74. if (real_size > size)
  75. return -ERANGE;
  76. ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  77. for (n=0; n < acl->a_count; n++, ext_entry++) {
  78. ext_entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  79. ext_entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  80. ext_entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
  81. }
  82. return real_size;
  83. }
  84. EXPORT_SYMBOL (posix_acl_to_xattr);