xattr.c 9.6 KB

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