xattr.c 9.7 KB

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