xattr_user.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * linux/fs/ext3/xattr_user.c
  3. * Handler for extended user attributes.
  4. *
  5. * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/string.h>
  9. #include <linux/fs.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/ext3_jbd.h>
  12. #include <linux/ext3_fs.h>
  13. #include "xattr.h"
  14. #define XATTR_USER_PREFIX "user."
  15. static size_t
  16. ext3_xattr_user_list(struct inode *inode, char *list, size_t list_size,
  17. const char *name, size_t name_len)
  18. {
  19. const size_t prefix_len = sizeof(XATTR_USER_PREFIX)-1;
  20. const size_t total_len = prefix_len + name_len + 1;
  21. if (!test_opt(inode->i_sb, XATTR_USER))
  22. return 0;
  23. if (list && total_len <= list_size) {
  24. memcpy(list, XATTR_USER_PREFIX, prefix_len);
  25. memcpy(list+prefix_len, name, name_len);
  26. list[prefix_len + name_len] = '\0';
  27. }
  28. return total_len;
  29. }
  30. static int
  31. ext3_xattr_user_get(struct inode *inode, const char *name,
  32. void *buffer, size_t size)
  33. {
  34. int error;
  35. if (strcmp(name, "") == 0)
  36. return -EINVAL;
  37. if (!test_opt(inode->i_sb, XATTR_USER))
  38. return -EOPNOTSUPP;
  39. error = permission(inode, MAY_READ, NULL);
  40. if (error)
  41. return error;
  42. return ext3_xattr_get(inode, EXT3_XATTR_INDEX_USER, name, buffer, size);
  43. }
  44. static int
  45. ext3_xattr_user_set(struct inode *inode, const char *name,
  46. const void *value, size_t size, int flags)
  47. {
  48. int error;
  49. if (strcmp(name, "") == 0)
  50. return -EINVAL;
  51. if (!test_opt(inode->i_sb, XATTR_USER))
  52. return -EOPNOTSUPP;
  53. if ( !S_ISREG(inode->i_mode) &&
  54. (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
  55. return -EPERM;
  56. error = permission(inode, MAY_WRITE, NULL);
  57. if (error)
  58. return error;
  59. return ext3_xattr_set(inode, EXT3_XATTR_INDEX_USER, name,
  60. value, size, flags);
  61. }
  62. struct xattr_handler ext3_xattr_user_handler = {
  63. .prefix = XATTR_USER_PREFIX,
  64. .list = ext3_xattr_user_list,
  65. .get = ext3_xattr_user_get,
  66. .set = ext3_xattr_user_set,
  67. };