xattr.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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) {
  44. ret = -ENODATA;
  45. goto out;
  46. } else if (IS_ERR(di)) {
  47. ret = PTR_ERR(di);
  48. goto out;
  49. }
  50. leaf = path->nodes[0];
  51. /* if size is 0, that means we want the size of the attr */
  52. if (!size) {
  53. ret = btrfs_dir_data_len(leaf, di);
  54. goto out;
  55. }
  56. /* now get the data out of our dir_item */
  57. if (btrfs_dir_data_len(leaf, di) > size) {
  58. ret = -ERANGE;
  59. goto out;
  60. }
  61. /*
  62. * The way things are packed into the leaf is like this
  63. * |struct btrfs_dir_item|name|data|
  64. * where name is the xattr name, so security.foo, and data is the
  65. * content of the xattr. data_ptr points to the location in memory
  66. * where the data starts in the in memory leaf
  67. */
  68. data_ptr = (unsigned long)((char *)(di + 1) +
  69. btrfs_dir_name_len(leaf, di));
  70. read_extent_buffer(leaf, buffer, data_ptr,
  71. btrfs_dir_data_len(leaf, di));
  72. ret = btrfs_dir_data_len(leaf, di);
  73. out:
  74. btrfs_free_path(path);
  75. return ret;
  76. }
  77. int __btrfs_setxattr(struct inode *inode, const char *name,
  78. const void *value, size_t size, int flags)
  79. {
  80. struct btrfs_dir_item *di;
  81. struct btrfs_root *root = BTRFS_I(inode)->root;
  82. struct btrfs_trans_handle *trans;
  83. struct btrfs_path *path;
  84. int ret = 0, mod = 0;
  85. path = btrfs_alloc_path();
  86. if (!path)
  87. return -ENOMEM;
  88. trans = btrfs_start_transaction(root, 1);
  89. btrfs_set_trans_block_group(trans, inode);
  90. /* first lets see if we already have this xattr */
  91. di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
  92. strlen(name), -1);
  93. if (IS_ERR(di)) {
  94. ret = PTR_ERR(di);
  95. goto out;
  96. }
  97. /* ok we already have this xattr, lets remove it */
  98. if (di) {
  99. /* if we want create only exit */
  100. if (flags & XATTR_CREATE) {
  101. ret = -EEXIST;
  102. goto out;
  103. }
  104. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  105. if (ret)
  106. goto out;
  107. btrfs_release_path(root, path);
  108. /* if we don't have a value then we are removing the xattr */
  109. if (!value) {
  110. mod = 1;
  111. goto out;
  112. }
  113. } else {
  114. btrfs_release_path(root, path);
  115. if (flags & XATTR_REPLACE) {
  116. /* we couldn't find the attr to replace */
  117. ret = -ENODATA;
  118. goto out;
  119. }
  120. }
  121. /* ok we have to create a completely new xattr */
  122. ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
  123. value, size, inode->i_ino);
  124. if (ret)
  125. goto out;
  126. mod = 1;
  127. out:
  128. if (mod) {
  129. inode->i_ctime = CURRENT_TIME;
  130. ret = btrfs_update_inode(trans, root, inode);
  131. }
  132. btrfs_end_transaction(trans, root);
  133. btrfs_free_path(path);
  134. return ret;
  135. }
  136. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  137. {
  138. struct btrfs_key key, found_key;
  139. struct inode *inode = dentry->d_inode;
  140. struct btrfs_root *root = BTRFS_I(inode)->root;
  141. struct btrfs_path *path;
  142. struct btrfs_item *item;
  143. struct extent_buffer *leaf;
  144. struct btrfs_dir_item *di;
  145. int ret = 0, slot, advance;
  146. size_t total_size = 0, size_left = size;
  147. unsigned long name_ptr;
  148. size_t name_len;
  149. u32 nritems;
  150. /*
  151. * ok we want all objects associated with this id.
  152. * NOTE: we set key.offset = 0; because we want to start with the
  153. * first xattr that we find and walk forward
  154. */
  155. key.objectid = inode->i_ino;
  156. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  157. key.offset = 0;
  158. path = btrfs_alloc_path();
  159. if (!path)
  160. return -ENOMEM;
  161. path->reada = 2;
  162. /* search for our xattrs */
  163. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  164. if (ret < 0)
  165. goto err;
  166. advance = 0;
  167. while (1) {
  168. leaf = path->nodes[0];
  169. nritems = btrfs_header_nritems(leaf);
  170. slot = path->slots[0];
  171. /* this is where we start walking through the path */
  172. if (advance || slot >= nritems) {
  173. /*
  174. * if we've reached the last slot in this leaf we need
  175. * to go to the next leaf and reset everything
  176. */
  177. if (slot >= nritems-1) {
  178. ret = btrfs_next_leaf(root, path);
  179. if (ret)
  180. break;
  181. leaf = path->nodes[0];
  182. nritems = btrfs_header_nritems(leaf);
  183. slot = path->slots[0];
  184. } else {
  185. /*
  186. * just walking through the slots on this leaf
  187. */
  188. slot++;
  189. path->slots[0]++;
  190. }
  191. }
  192. advance = 1;
  193. item = btrfs_item_nr(leaf, slot);
  194. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  195. /* check to make sure this item is what we want */
  196. if (found_key.objectid != key.objectid)
  197. break;
  198. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  199. break;
  200. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  201. name_len = btrfs_dir_name_len(leaf, di);
  202. total_size += name_len + 1;
  203. /* we are just looking for how big our buffer needs to be */
  204. if (!size)
  205. continue;
  206. if (!buffer || (name_len + 1) > size_left) {
  207. ret = -ERANGE;
  208. goto err;
  209. }
  210. name_ptr = (unsigned long)(di + 1);
  211. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  212. buffer[name_len] = '\0';
  213. size_left -= name_len + 1;
  214. buffer += name_len + 1;
  215. }
  216. ret = total_size;
  217. err:
  218. btrfs_free_path(path);
  219. return ret;
  220. }
  221. /*
  222. * List of handlers for synthetic system.* attributes. All real ondisk
  223. * attributes are handled directly.
  224. */
  225. struct xattr_handler *btrfs_xattr_handlers[] = {
  226. #ifdef CONFIG_FS_POSIX_ACL
  227. &btrfs_xattr_acl_access_handler,
  228. &btrfs_xattr_acl_default_handler,
  229. #endif
  230. NULL,
  231. };
  232. /*
  233. * Check if the attribute is in a supported namespace.
  234. *
  235. * This applied after the check for the synthetic attributes in the system
  236. * namespace.
  237. */
  238. static bool btrfs_is_valid_xattr(const char *name)
  239. {
  240. return !strncmp(name, XATTR_SECURITY_PREFIX,
  241. XATTR_SECURITY_PREFIX_LEN) ||
  242. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
  243. !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  244. !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  245. }
  246. ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
  247. void *buffer, size_t size)
  248. {
  249. /*
  250. * If this is a request for a synthetic attribute in the system.*
  251. * namespace use the generic infrastructure to resolve a handler
  252. * for it via sb->s_xattr.
  253. */
  254. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  255. return generic_getxattr(dentry, name, buffer, size);
  256. if (!btrfs_is_valid_xattr(name))
  257. return -EOPNOTSUPP;
  258. return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
  259. }
  260. int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  261. size_t size, int flags)
  262. {
  263. /*
  264. * If this is a request for a synthetic attribute in the system.*
  265. * namespace use the generic infrastructure to resolve a handler
  266. * for it via sb->s_xattr.
  267. */
  268. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  269. return generic_setxattr(dentry, name, value, size, flags);
  270. if (!btrfs_is_valid_xattr(name))
  271. return -EOPNOTSUPP;
  272. if (size == 0)
  273. value = ""; /* empty EA, do not remove */
  274. return __btrfs_setxattr(dentry->d_inode, name, value, size, flags);
  275. }
  276. int btrfs_removexattr(struct dentry *dentry, const char *name)
  277. {
  278. /*
  279. * If this is a request for a synthetic attribute in the system.*
  280. * namespace use the generic infrastructure to resolve a handler
  281. * for it via sb->s_xattr.
  282. */
  283. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  284. return generic_removexattr(dentry, name);
  285. if (!btrfs_is_valid_xattr(name))
  286. return -EOPNOTSUPP;
  287. return __btrfs_setxattr(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
  288. }