xattr_acl.c 2.3 KB

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