xattr.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. ret = btrfs_reserve_metadata_space(root, 2);
  137. if (ret)
  138. return ret;
  139. trans = btrfs_start_transaction(root, 1);
  140. if (!trans) {
  141. ret = -ENOMEM;
  142. goto out;
  143. }
  144. btrfs_set_trans_block_group(trans, inode);
  145. ret = do_setxattr(trans, inode, name, value, size, flags);
  146. if (ret)
  147. goto out;
  148. inode->i_ctime = CURRENT_TIME;
  149. ret = btrfs_update_inode(trans, root, inode);
  150. BUG_ON(ret);
  151. out:
  152. btrfs_end_transaction_throttle(trans, root);
  153. btrfs_unreserve_metadata_space(root, 2);
  154. return ret;
  155. }
  156. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  157. {
  158. struct btrfs_key key, found_key;
  159. struct inode *inode = dentry->d_inode;
  160. struct btrfs_root *root = BTRFS_I(inode)->root;
  161. struct btrfs_path *path;
  162. struct btrfs_item *item;
  163. struct extent_buffer *leaf;
  164. struct btrfs_dir_item *di;
  165. int ret = 0, slot, advance;
  166. size_t total_size = 0, size_left = size;
  167. unsigned long name_ptr;
  168. size_t name_len;
  169. u32 nritems;
  170. /*
  171. * ok we want all objects associated with this id.
  172. * NOTE: we set key.offset = 0; because we want to start with the
  173. * first xattr that we find and walk forward
  174. */
  175. key.objectid = inode->i_ino;
  176. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  177. key.offset = 0;
  178. path = btrfs_alloc_path();
  179. if (!path)
  180. return -ENOMEM;
  181. path->reada = 2;
  182. /* search for our xattrs */
  183. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  184. if (ret < 0)
  185. goto err;
  186. advance = 0;
  187. while (1) {
  188. leaf = path->nodes[0];
  189. nritems = btrfs_header_nritems(leaf);
  190. slot = path->slots[0];
  191. /* this is where we start walking through the path */
  192. if (advance || slot >= nritems) {
  193. /*
  194. * if we've reached the last slot in this leaf we need
  195. * to go to the next leaf and reset everything
  196. */
  197. if (slot >= nritems-1) {
  198. ret = btrfs_next_leaf(root, path);
  199. if (ret)
  200. break;
  201. leaf = path->nodes[0];
  202. nritems = btrfs_header_nritems(leaf);
  203. slot = path->slots[0];
  204. } else {
  205. /*
  206. * just walking through the slots on this leaf
  207. */
  208. slot++;
  209. path->slots[0]++;
  210. }
  211. }
  212. advance = 1;
  213. item = btrfs_item_nr(leaf, slot);
  214. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  215. /* check to make sure this item is what we want */
  216. if (found_key.objectid != key.objectid)
  217. break;
  218. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  219. break;
  220. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  221. name_len = btrfs_dir_name_len(leaf, di);
  222. total_size += name_len + 1;
  223. /* we are just looking for how big our buffer needs to be */
  224. if (!size)
  225. continue;
  226. if (!buffer || (name_len + 1) > size_left) {
  227. ret = -ERANGE;
  228. goto err;
  229. }
  230. name_ptr = (unsigned long)(di + 1);
  231. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  232. buffer[name_len] = '\0';
  233. size_left -= name_len + 1;
  234. buffer += name_len + 1;
  235. }
  236. ret = total_size;
  237. err:
  238. btrfs_free_path(path);
  239. return ret;
  240. }
  241. /*
  242. * List of handlers for synthetic system.* attributes. All real ondisk
  243. * attributes are handled directly.
  244. */
  245. struct xattr_handler *btrfs_xattr_handlers[] = {
  246. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  247. &btrfs_xattr_acl_access_handler,
  248. &btrfs_xattr_acl_default_handler,
  249. #endif
  250. NULL,
  251. };
  252. /*
  253. * Check if the attribute is in a supported namespace.
  254. *
  255. * This applied after the check for the synthetic attributes in the system
  256. * namespace.
  257. */
  258. static bool btrfs_is_valid_xattr(const char *name)
  259. {
  260. return !strncmp(name, XATTR_SECURITY_PREFIX,
  261. XATTR_SECURITY_PREFIX_LEN) ||
  262. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
  263. !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  264. !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  265. }
  266. ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
  267. void *buffer, size_t size)
  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_getxattr(dentry, name, buffer, size);
  276. if (!btrfs_is_valid_xattr(name))
  277. return -EOPNOTSUPP;
  278. return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
  279. }
  280. int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  281. size_t size, int flags)
  282. {
  283. /*
  284. * If this is a request for a synthetic attribute in the system.*
  285. * namespace use the generic infrastructure to resolve a handler
  286. * for it via sb->s_xattr.
  287. */
  288. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  289. return generic_setxattr(dentry, name, value, size, flags);
  290. if (!btrfs_is_valid_xattr(name))
  291. return -EOPNOTSUPP;
  292. if (size == 0)
  293. value = ""; /* empty EA, do not remove */
  294. return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
  295. flags);
  296. }
  297. int btrfs_removexattr(struct dentry *dentry, const char *name)
  298. {
  299. /*
  300. * If this is a request for a synthetic attribute in the system.*
  301. * namespace use the generic infrastructure to resolve a handler
  302. * for it via sb->s_xattr.
  303. */
  304. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  305. return generic_removexattr(dentry, name);
  306. if (!btrfs_is_valid_xattr(name))
  307. return -EOPNOTSUPP;
  308. return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
  309. XATTR_REPLACE);
  310. }
  311. int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
  312. struct inode *inode, struct inode *dir)
  313. {
  314. int err;
  315. size_t len;
  316. void *value;
  317. char *suffix;
  318. char *name;
  319. err = security_inode_init_security(inode, dir, &suffix, &value, &len);
  320. if (err) {
  321. if (err == -EOPNOTSUPP)
  322. return 0;
  323. return err;
  324. }
  325. name = kmalloc(XATTR_SECURITY_PREFIX_LEN + strlen(suffix) + 1,
  326. GFP_NOFS);
  327. if (!name) {
  328. err = -ENOMEM;
  329. } else {
  330. strcpy(name, XATTR_SECURITY_PREFIX);
  331. strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
  332. err = __btrfs_setxattr(trans, inode, name, value, len, 0);
  333. kfree(name);
  334. }
  335. kfree(suffix);
  336. kfree(value);
  337. return err;
  338. }