attr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hfs/attr.c
  4. *
  5. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  6. *
  7. * Export hfs data via xattr
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/xattr.h>
  11. #include "hfs_fs.h"
  12. #include "btree.h"
  13. enum hfs_xattr_type {
  14. HFS_TYPE,
  15. HFS_CREATOR,
  16. };
  17. static int __hfs_setxattr(struct inode *inode, enum hfs_xattr_type type,
  18. const void *value, size_t size, int flags)
  19. {
  20. struct hfs_find_data fd;
  21. hfs_cat_rec rec;
  22. struct hfs_cat_file *file;
  23. int res;
  24. if (!S_ISREG(inode->i_mode) || HFS_IS_RSRC(inode))
  25. return -EOPNOTSUPP;
  26. res = hfs_find_init(HFS_SB(inode->i_sb)->cat_tree, &fd);
  27. if (res)
  28. return res;
  29. fd.search_key->cat = HFS_I(inode)->cat_key;
  30. res = hfs_brec_find(&fd);
  31. if (res)
  32. goto out;
  33. hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
  34. sizeof(struct hfs_cat_file));
  35. file = &rec.file;
  36. switch (type) {
  37. case HFS_TYPE:
  38. if (size == 4)
  39. memcpy(&file->UsrWds.fdType, value, 4);
  40. else
  41. res = -ERANGE;
  42. break;
  43. case HFS_CREATOR:
  44. if (size == 4)
  45. memcpy(&file->UsrWds.fdCreator, value, 4);
  46. else
  47. res = -ERANGE;
  48. break;
  49. }
  50. if (!res)
  51. hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
  52. sizeof(struct hfs_cat_file));
  53. out:
  54. hfs_find_exit(&fd);
  55. return res;
  56. }
  57. static ssize_t __hfs_getxattr(struct inode *inode, enum hfs_xattr_type type,
  58. void *value, size_t size)
  59. {
  60. struct hfs_find_data fd;
  61. hfs_cat_rec rec;
  62. struct hfs_cat_file *file;
  63. ssize_t res = 0;
  64. if (!S_ISREG(inode->i_mode) || HFS_IS_RSRC(inode))
  65. return -EOPNOTSUPP;
  66. if (size) {
  67. res = hfs_find_init(HFS_SB(inode->i_sb)->cat_tree, &fd);
  68. if (res)
  69. return res;
  70. fd.search_key->cat = HFS_I(inode)->cat_key;
  71. res = hfs_brec_find(&fd);
  72. if (res)
  73. goto out;
  74. hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
  75. sizeof(struct hfs_cat_file));
  76. }
  77. file = &rec.file;
  78. switch (type) {
  79. case HFS_TYPE:
  80. if (size >= 4) {
  81. memcpy(value, &file->UsrWds.fdType, 4);
  82. res = 4;
  83. } else
  84. res = size ? -ERANGE : 4;
  85. break;
  86. case HFS_CREATOR:
  87. if (size >= 4) {
  88. memcpy(value, &file->UsrWds.fdCreator, 4);
  89. res = 4;
  90. } else
  91. res = size ? -ERANGE : 4;
  92. break;
  93. }
  94. out:
  95. if (size)
  96. hfs_find_exit(&fd);
  97. return res;
  98. }
  99. static int hfs_xattr_get(const struct xattr_handler *handler,
  100. struct dentry *unused, struct inode *inode,
  101. const char *name, void *value, size_t size, int flags)
  102. {
  103. return __hfs_getxattr(inode, handler->flags, value, size);
  104. }
  105. static int hfs_xattr_set(const struct xattr_handler *handler,
  106. struct dentry *unused, struct inode *inode,
  107. const char *name, const void *value, size_t size,
  108. int flags)
  109. {
  110. if (!value)
  111. return -EOPNOTSUPP;
  112. return __hfs_setxattr(inode, handler->flags, value, size, flags);
  113. }
  114. static const struct xattr_handler hfs_creator_handler = {
  115. .name = "hfs.creator",
  116. .flags = HFS_CREATOR,
  117. .get = hfs_xattr_get,
  118. .set = hfs_xattr_set,
  119. };
  120. static const struct xattr_handler hfs_type_handler = {
  121. .name = "hfs.type",
  122. .flags = HFS_TYPE,
  123. .get = hfs_xattr_get,
  124. .set = hfs_xattr_set,
  125. };
  126. const struct xattr_handler *hfs_xattr_handlers[] = {
  127. &hfs_creator_handler,
  128. &hfs_type_handler,
  129. NULL
  130. };