xattr.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2007 Red Hat. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/fs.h>
  20. #include <linux/slab.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/xattr.h>
  23. #include "ctree.h"
  24. #include "btrfs_inode.h"
  25. #include "transaction.h"
  26. #include "xattr.h"
  27. #include "disk-io.h"
  28. ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
  29. void *buffer, size_t size)
  30. {
  31. struct btrfs_dir_item *di;
  32. struct btrfs_root *root = BTRFS_I(inode)->root;
  33. struct btrfs_path *path;
  34. struct extent_buffer *leaf;
  35. int ret = 0;
  36. unsigned long data_ptr;
  37. path = btrfs_alloc_path();
  38. if (!path)
  39. return -ENOMEM;
  40. /* lookup the xattr by name */
  41. di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
  42. strlen(name), 0);
  43. if (!di || IS_ERR(di)) {
  44. ret = -ENODATA;
  45. goto out;
  46. }
  47. leaf = path->nodes[0];
  48. /* if size is 0, that means we want the size of the attr */
  49. if (!size) {
  50. ret = btrfs_dir_data_len(leaf, di);
  51. goto out;
  52. }
  53. /* now get the data out of our dir_item */
  54. if (btrfs_dir_data_len(leaf, di) > size) {
  55. ret = -ERANGE;
  56. goto out;
  57. }
  58. data_ptr = (unsigned long)((char *)(di + 1) +
  59. btrfs_dir_name_len(leaf, di));
  60. read_extent_buffer(leaf, buffer, data_ptr,
  61. btrfs_dir_data_len(leaf, di));
  62. ret = btrfs_dir_data_len(leaf, di);
  63. out:
  64. btrfs_free_path(path);
  65. return ret;
  66. }
  67. int __btrfs_setxattr(struct inode *inode, const char *name,
  68. const void *value, size_t size, int flags)
  69. {
  70. struct btrfs_dir_item *di;
  71. struct btrfs_root *root = BTRFS_I(inode)->root;
  72. struct btrfs_trans_handle *trans;
  73. struct btrfs_path *path;
  74. int ret = 0, mod = 0;
  75. path = btrfs_alloc_path();
  76. if (!path)
  77. return -ENOMEM;
  78. trans = btrfs_start_transaction(root, 1);
  79. btrfs_set_trans_block_group(trans, inode);
  80. /* first lets see if we already have this xattr */
  81. di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
  82. strlen(name), -1);
  83. if (IS_ERR(di)) {
  84. ret = PTR_ERR(di);
  85. goto out;
  86. }
  87. /* ok we already have this xattr, lets remove it */
  88. if (di) {
  89. /* if we want create only exit */
  90. if (flags & XATTR_CREATE) {
  91. ret = -EEXIST;
  92. goto out;
  93. }
  94. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  95. if (ret)
  96. goto out;
  97. btrfs_release_path(root, path);
  98. /* if we don't have a value then we are removing the xattr */
  99. if (!value) {
  100. mod = 1;
  101. goto out;
  102. }
  103. } else {
  104. btrfs_release_path(root, path);
  105. if (flags & XATTR_REPLACE) {
  106. /* we couldn't find the attr to replace */
  107. ret = -ENODATA;
  108. goto out;
  109. }
  110. }
  111. /* ok we have to create a completely new xattr */
  112. ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
  113. value, size, inode->i_ino);
  114. if (ret)
  115. goto out;
  116. mod = 1;
  117. out:
  118. if (mod) {
  119. inode->i_ctime = CURRENT_TIME;
  120. ret = btrfs_update_inode(trans, root, inode);
  121. }
  122. btrfs_end_transaction(trans, root);
  123. btrfs_free_path(path);
  124. return ret;
  125. }
  126. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  127. {
  128. struct btrfs_key key, found_key;
  129. struct inode *inode = dentry->d_inode;
  130. struct btrfs_root *root = BTRFS_I(inode)->root;
  131. struct btrfs_path *path;
  132. struct btrfs_item *item;
  133. struct extent_buffer *leaf;
  134. struct btrfs_dir_item *di;
  135. int ret = 0, slot, advance;
  136. size_t total_size = 0, size_left = size;
  137. unsigned long name_ptr;
  138. size_t name_len;
  139. u32 nritems;
  140. /*
  141. * ok we want all objects associated with this id.
  142. * NOTE: we set key.offset = 0; because we want to start with the
  143. * first xattr that we find and walk forward
  144. */
  145. key.objectid = inode->i_ino;
  146. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  147. key.offset = 0;
  148. path = btrfs_alloc_path();
  149. if (!path)
  150. return -ENOMEM;
  151. path->reada = 2;
  152. /* search for our xattrs */
  153. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  154. if (ret < 0)
  155. goto err;
  156. ret = 0;
  157. advance = 0;
  158. while (1) {
  159. leaf = path->nodes[0];
  160. nritems = btrfs_header_nritems(leaf);
  161. slot = path->slots[0];
  162. /* this is where we start walking through the path */
  163. if (advance || slot >= nritems) {
  164. /*
  165. * if we've reached the last slot in this leaf we need
  166. * to go to the next leaf and reset everything
  167. */
  168. if (slot >= nritems-1) {
  169. ret = btrfs_next_leaf(root, path);
  170. if (ret)
  171. break;
  172. leaf = path->nodes[0];
  173. nritems = btrfs_header_nritems(leaf);
  174. slot = path->slots[0];
  175. } else {
  176. /*
  177. * just walking through the slots on this leaf
  178. */
  179. slot++;
  180. path->slots[0]++;
  181. }
  182. }
  183. advance = 1;
  184. item = btrfs_item_nr(leaf, slot);
  185. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  186. /* check to make sure this item is what we want */
  187. if (found_key.objectid != key.objectid)
  188. break;
  189. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  190. break;
  191. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  192. name_len = btrfs_dir_name_len(leaf, di);
  193. total_size += name_len + 1;
  194. /* we are just looking for how big our buffer needs to be */
  195. if (!size)
  196. continue;
  197. if (!buffer || (name_len + 1) > size_left) {
  198. ret = -ERANGE;
  199. goto err;
  200. }
  201. name_ptr = (unsigned long)(di + 1);
  202. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  203. buffer[name_len] = '\0';
  204. size_left -= name_len + 1;
  205. buffer += name_len + 1;
  206. }
  207. ret = total_size;
  208. err:
  209. btrfs_free_path(path);
  210. return ret;
  211. }
  212. /*
  213. * List of handlers for synthetic system.* attributes. All real ondisk
  214. * attributes are handled directly.
  215. */
  216. struct xattr_handler *btrfs_xattr_handlers[] = {
  217. #ifdef CONFIG_FS_POSIX_ACL
  218. &btrfs_xattr_acl_access_handler,
  219. &btrfs_xattr_acl_default_handler,
  220. #endif
  221. NULL,
  222. };
  223. /*
  224. * Check if the attribute is in a supported namespace.
  225. *
  226. * This applied after the check for the synthetic attributes in the system
  227. * namespace.
  228. */
  229. static bool btrfs_is_valid_xattr(const char *name)
  230. {
  231. return !strncmp(name, XATTR_SECURITY_PREFIX,
  232. XATTR_SECURITY_PREFIX_LEN) ||
  233. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
  234. !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  235. !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  236. }
  237. ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
  238. void *buffer, size_t size)
  239. {
  240. /*
  241. * If this is a request for a synthetic attribute in the system.*
  242. * namespace use the generic infrastructure to resolve a handler
  243. * for it via sb->s_xattr.
  244. */
  245. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  246. return generic_getxattr(dentry, name, buffer, size);
  247. if (!btrfs_is_valid_xattr(name))
  248. return -EOPNOTSUPP;
  249. return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
  250. }
  251. int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  252. size_t size, int flags)
  253. {
  254. /*
  255. * If this is a request for a synthetic attribute in the system.*
  256. * namespace use the generic infrastructure to resolve a handler
  257. * for it via sb->s_xattr.
  258. */
  259. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  260. return generic_setxattr(dentry, name, value, size, flags);
  261. if (!btrfs_is_valid_xattr(name))
  262. return -EOPNOTSUPP;
  263. if (size == 0)
  264. value = ""; /* empty EA, do not remove */
  265. return __btrfs_setxattr(dentry->d_inode, name, value, size, flags);
  266. }
  267. int btrfs_removexattr(struct dentry *dentry, const char *name)
  268. {
  269. /*
  270. * If this is a request for a synthetic attribute in the system.*
  271. * namespace use the generic infrastructure to resolve a handler
  272. * for it via sb->s_xattr.
  273. */
  274. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  275. return generic_removexattr(dentry, name);
  276. if (!btrfs_is_valid_xattr(name))
  277. return -EOPNOTSUPP;
  278. return __btrfs_setxattr(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
  279. }