xfs_xattr.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2008 Christoph Hellwig.
  3. * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would 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, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_da_btree.h"
  20. #include "xfs_bmap_btree.h"
  21. #include "xfs_inode.h"
  22. #include "xfs_attr.h"
  23. #include "xfs_attr_leaf.h"
  24. #include "xfs_acl.h"
  25. #include "xfs_vnodeops.h"
  26. #include <linux/posix_acl_xattr.h>
  27. #include <linux/xattr.h>
  28. /*
  29. * ACL handling. Should eventually be moved into xfs_acl.c
  30. */
  31. static int
  32. xfs_decode_acl(const char *name)
  33. {
  34. if (strcmp(name, "posix_acl_access") == 0)
  35. return _ACL_TYPE_ACCESS;
  36. else if (strcmp(name, "posix_acl_default") == 0)
  37. return _ACL_TYPE_DEFAULT;
  38. return -EINVAL;
  39. }
  40. /*
  41. * Get system extended attributes which at the moment only
  42. * includes Posix ACLs.
  43. */
  44. static int
  45. xfs_xattr_system_get(struct inode *inode, const char *name,
  46. void *buffer, size_t size)
  47. {
  48. int acl;
  49. acl = xfs_decode_acl(name);
  50. if (acl < 0)
  51. return acl;
  52. return xfs_acl_vget(inode, buffer, size, acl);
  53. }
  54. static int
  55. xfs_xattr_system_set(struct inode *inode, const char *name,
  56. const void *value, size_t size, int flags)
  57. {
  58. int acl;
  59. acl = xfs_decode_acl(name);
  60. if (acl < 0)
  61. return acl;
  62. if (flags & XATTR_CREATE)
  63. return -EINVAL;
  64. if (!value)
  65. return xfs_acl_vremove(inode, acl);
  66. return xfs_acl_vset(inode, (void *)value, size, acl);
  67. }
  68. static struct xattr_handler xfs_xattr_system_handler = {
  69. .prefix = XATTR_SYSTEM_PREFIX,
  70. .get = xfs_xattr_system_get,
  71. .set = xfs_xattr_system_set,
  72. };
  73. /*
  74. * Real xattr handling. The only difference between the namespaces is
  75. * a flag passed to the low-level attr code.
  76. */
  77. static int
  78. __xfs_xattr_get(struct inode *inode, const char *name,
  79. void *value, size_t size, int xflags)
  80. {
  81. struct xfs_inode *ip = XFS_I(inode);
  82. int error, asize = size;
  83. if (strcmp(name, "") == 0)
  84. return -EINVAL;
  85. /* Convert Linux syscall to XFS internal ATTR flags */
  86. if (!size) {
  87. xflags |= ATTR_KERNOVAL;
  88. value = NULL;
  89. }
  90. error = -xfs_attr_get(ip, name, value, &asize, xflags);
  91. if (error)
  92. return error;
  93. return asize;
  94. }
  95. static int
  96. __xfs_xattr_set(struct inode *inode, const char *name, const void *value,
  97. size_t size, int flags, int xflags)
  98. {
  99. struct xfs_inode *ip = XFS_I(inode);
  100. if (strcmp(name, "") == 0)
  101. return -EINVAL;
  102. /* Convert Linux syscall to XFS internal ATTR flags */
  103. if (flags & XATTR_CREATE)
  104. xflags |= ATTR_CREATE;
  105. if (flags & XATTR_REPLACE)
  106. xflags |= ATTR_REPLACE;
  107. if (!value)
  108. return -xfs_attr_remove(ip, name, xflags);
  109. return -xfs_attr_set(ip, name, (void *)value, size, xflags);
  110. }
  111. static int
  112. xfs_xattr_user_get(struct inode *inode, const char *name,
  113. void *value, size_t size)
  114. {
  115. return __xfs_xattr_get(inode, name, value, size, 0);
  116. }
  117. static int
  118. xfs_xattr_user_set(struct inode *inode, const char *name,
  119. const void *value, size_t size, int flags)
  120. {
  121. return __xfs_xattr_set(inode, name, value, size, flags, 0);
  122. }
  123. static struct xattr_handler xfs_xattr_user_handler = {
  124. .prefix = XATTR_USER_PREFIX,
  125. .get = xfs_xattr_user_get,
  126. .set = xfs_xattr_user_set,
  127. };
  128. static int
  129. xfs_xattr_trusted_get(struct inode *inode, const char *name,
  130. void *value, size_t size)
  131. {
  132. return __xfs_xattr_get(inode, name, value, size, ATTR_ROOT);
  133. }
  134. static int
  135. xfs_xattr_trusted_set(struct inode *inode, const char *name,
  136. const void *value, size_t size, int flags)
  137. {
  138. return __xfs_xattr_set(inode, name, value, size, flags, ATTR_ROOT);
  139. }
  140. static struct xattr_handler xfs_xattr_trusted_handler = {
  141. .prefix = XATTR_TRUSTED_PREFIX,
  142. .get = xfs_xattr_trusted_get,
  143. .set = xfs_xattr_trusted_set,
  144. };
  145. static int
  146. xfs_xattr_secure_get(struct inode *inode, const char *name,
  147. void *value, size_t size)
  148. {
  149. return __xfs_xattr_get(inode, name, value, size, ATTR_SECURE);
  150. }
  151. static int
  152. xfs_xattr_secure_set(struct inode *inode, const char *name,
  153. const void *value, size_t size, int flags)
  154. {
  155. return __xfs_xattr_set(inode, name, value, size, flags, ATTR_SECURE);
  156. }
  157. static struct xattr_handler xfs_xattr_security_handler = {
  158. .prefix = XATTR_SECURITY_PREFIX,
  159. .get = xfs_xattr_secure_get,
  160. .set = xfs_xattr_secure_set,
  161. };
  162. struct xattr_handler *xfs_xattr_handlers[] = {
  163. &xfs_xattr_user_handler,
  164. &xfs_xattr_trusted_handler,
  165. &xfs_xattr_security_handler,
  166. &xfs_xattr_system_handler,
  167. NULL
  168. };
  169. static unsigned int xfs_xattr_prefix_len(int flags)
  170. {
  171. if (flags & XFS_ATTR_SECURE)
  172. return sizeof("security");
  173. else if (flags & XFS_ATTR_ROOT)
  174. return sizeof("trusted");
  175. else
  176. return sizeof("user");
  177. }
  178. static const char *xfs_xattr_prefix(int flags)
  179. {
  180. if (flags & XFS_ATTR_SECURE)
  181. return xfs_xattr_security_handler.prefix;
  182. else if (flags & XFS_ATTR_ROOT)
  183. return xfs_xattr_trusted_handler.prefix;
  184. else
  185. return xfs_xattr_user_handler.prefix;
  186. }
  187. static int
  188. xfs_xattr_put_listent(struct xfs_attr_list_context *context, int flags,
  189. char *name, int namelen, int valuelen, char *value)
  190. {
  191. unsigned int prefix_len = xfs_xattr_prefix_len(flags);
  192. char *offset;
  193. int arraytop;
  194. ASSERT(context->count >= 0);
  195. /*
  196. * Only show root namespace entries if we are actually allowed to
  197. * see them.
  198. */
  199. if ((flags & XFS_ATTR_ROOT) && !capable(CAP_SYS_ADMIN))
  200. return 0;
  201. arraytop = context->count + prefix_len + namelen + 1;
  202. if (arraytop > context->firstu) {
  203. context->count = -1; /* insufficient space */
  204. return 1;
  205. }
  206. offset = (char *)context->alist + context->count;
  207. strncpy(offset, xfs_xattr_prefix(flags), prefix_len);
  208. offset += prefix_len;
  209. strncpy(offset, name, namelen); /* real name */
  210. offset += namelen;
  211. *offset = '\0';
  212. context->count += prefix_len + namelen + 1;
  213. return 0;
  214. }
  215. static int
  216. xfs_xattr_put_listent_sizes(struct xfs_attr_list_context *context, int flags,
  217. char *name, int namelen, int valuelen, char *value)
  218. {
  219. context->count += xfs_xattr_prefix_len(flags) + namelen + 1;
  220. return 0;
  221. }
  222. static int
  223. list_one_attr(const char *name, const size_t len, void *data,
  224. size_t size, ssize_t *result)
  225. {
  226. char *p = data + *result;
  227. *result += len;
  228. if (!size)
  229. return 0;
  230. if (*result > size)
  231. return -ERANGE;
  232. strcpy(p, name);
  233. return 0;
  234. }
  235. ssize_t
  236. xfs_vn_listxattr(struct dentry *dentry, char *data, size_t size)
  237. {
  238. struct xfs_attr_list_context context;
  239. struct attrlist_cursor_kern cursor = { 0 };
  240. struct inode *inode = dentry->d_inode;
  241. int error;
  242. /*
  243. * First read the regular on-disk attributes.
  244. */
  245. memset(&context, 0, sizeof(context));
  246. context.dp = XFS_I(inode);
  247. context.cursor = &cursor;
  248. context.resynch = 1;
  249. context.alist = data;
  250. context.bufsize = size;
  251. context.firstu = context.bufsize;
  252. if (size)
  253. context.put_listent = xfs_xattr_put_listent;
  254. else
  255. context.put_listent = xfs_xattr_put_listent_sizes;
  256. xfs_attr_list_int(&context);
  257. if (context.count < 0)
  258. return -ERANGE;
  259. /*
  260. * Then add the two synthetic ACL attributes.
  261. */
  262. if (xfs_acl_vhasacl_access(inode)) {
  263. error = list_one_attr(POSIX_ACL_XATTR_ACCESS,
  264. strlen(POSIX_ACL_XATTR_ACCESS) + 1,
  265. data, size, &context.count);
  266. if (error)
  267. return error;
  268. }
  269. if (xfs_acl_vhasacl_default(inode)) {
  270. error = list_one_attr(POSIX_ACL_XATTR_DEFAULT,
  271. strlen(POSIX_ACL_XATTR_DEFAULT) + 1,
  272. data, size, &context.count);
  273. if (error)
  274. return error;
  275. }
  276. return context.count;
  277. }