xattr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /************************************************************************/
  18. /* */
  19. /* PROJECT : exFAT & FAT12/16/32 File System */
  20. /* FILE : xattr.c */
  21. /* PURPOSE : sdFAT code for supporting xattr(Extended File Attributes) */
  22. /* */
  23. /*----------------------------------------------------------------------*/
  24. /* NOTES */
  25. /* */
  26. /* */
  27. /************************************************************************/
  28. #include <linux/file.h>
  29. #include <linux/fs.h>
  30. #include <linux/xattr.h>
  31. #include <linux/dcache.h>
  32. #include "sdfat.h"
  33. #ifndef CONFIG_SDFAT_VIRTUAL_XATTR_SELINUX_LABEL
  34. #define CONFIG_SDFAT_VIRTUAL_XATTR_SELINUX_LABEL ("undefined")
  35. #endif
  36. static const char default_xattr[] = CONFIG_SDFAT_VIRTUAL_XATTR_SELINUX_LABEL;
  37. static int can_support(const char *name)
  38. {
  39. if (!name || strcmp(name, "security.selinux"))
  40. return -1;
  41. return 0;
  42. }
  43. ssize_t sdfat_listxattr(struct dentry *dentry, char *list, size_t size)
  44. {
  45. return 0;
  46. }
  47. /*************************************************************************
  48. * INNER FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  49. *************************************************************************/
  50. static int __sdfat_xattr_check_support(const char *name)
  51. {
  52. if (can_support(name))
  53. return -EOPNOTSUPP;
  54. return 0;
  55. }
  56. ssize_t __sdfat_getxattr(const char *name, void *value, size_t size)
  57. {
  58. if (can_support(name))
  59. return -EOPNOTSUPP;
  60. if ((size > strlen(default_xattr)+1) && value)
  61. strcpy(value, default_xattr);
  62. return strlen(default_xattr);
  63. }
  64. /*************************************************************************
  65. * FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  66. *************************************************************************/
  67. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  68. #if defined(CONFIG_ANDROID) && (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0))
  69. static int sdfat_xattr_get(const struct xattr_handler *handler,
  70. struct dentry *dentry, struct inode *inode,
  71. const char *name, void *buffer, size_t size,
  72. int flags)
  73. {
  74. return __sdfat_getxattr(name, buffer, size);
  75. }
  76. #else
  77. static int sdfat_xattr_get(const struct xattr_handler *handler,
  78. struct dentry *dentry, struct inode *inode,
  79. const char *name, void *buffer, size_t size)
  80. {
  81. return __sdfat_getxattr(name, buffer, size);
  82. }
  83. #endif
  84. static int sdfat_xattr_set(const struct xattr_handler *handler,
  85. struct dentry *dentry, struct inode *inode,
  86. const char *name, const void *value, size_t size,
  87. int flags)
  88. {
  89. return __sdfat_xattr_check_support(name);
  90. }
  91. const struct xattr_handler sdfat_xattr_handler = {
  92. .prefix = "", /* match anything */
  93. .get = sdfat_xattr_get,
  94. .set = sdfat_xattr_set,
  95. };
  96. const struct xattr_handler *sdfat_xattr_handlers[] = {
  97. &sdfat_xattr_handler,
  98. NULL
  99. };
  100. void setup_sdfat_xattr_handler(struct super_block *sb)
  101. {
  102. sb->s_xattr = sdfat_xattr_handlers;
  103. }
  104. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) */
  105. int sdfat_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
  106. {
  107. return __sdfat_xattr_check_support(name);
  108. }
  109. ssize_t sdfat_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
  110. {
  111. return __sdfat_getxattr(name, value, size);
  112. }
  113. int sdfat_removexattr(struct dentry *dentry, const char *name)
  114. {
  115. return __sdfat_xattr_check_support(name);
  116. }
  117. void setup_sdfat_xattr_handler(struct super_block *sb)
  118. {
  119. /* DO NOTHING */
  120. }
  121. #endif