xattr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. if (flags & XATTR_REPLACE) {
  93. di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), name,
  94. name_len, -1);
  95. if (IS_ERR(di)) {
  96. ret = PTR_ERR(di);
  97. goto out;
  98. } else if (!di) {
  99. ret = -ENODATA;
  100. goto out;
  101. }
  102. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  103. if (ret)
  104. goto out;
  105. btrfs_release_path(path);
  106. /*
  107. * remove the attribute
  108. */
  109. if (!value)
  110. goto out;
  111. }
  112. again:
  113. ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
  114. name, name_len, value, size);
  115. /*
  116. * If we're setting an xattr to a new value but the new value is say
  117. * exactly BTRFS_MAX_XATTR_SIZE, we could end up with EOVERFLOW getting
  118. * back from split_leaf. This is because it thinks we'll be extending
  119. * the existing item size, but we're asking for enough space to add the
  120. * item itself. So if we get EOVERFLOW just set ret to EEXIST and let
  121. * the rest of the function figure it out.
  122. */
  123. if (ret == -EOVERFLOW)
  124. ret = -EEXIST;
  125. if (ret == -EEXIST) {
  126. if (flags & XATTR_CREATE)
  127. goto out;
  128. /*
  129. * We can't use the path we already have since we won't have the
  130. * proper locking for a delete, so release the path and
  131. * re-lookup to delete the thing.
  132. */
  133. btrfs_release_path(path);
  134. di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode),
  135. name, name_len, -1);
  136. if (IS_ERR(di)) {
  137. ret = PTR_ERR(di);
  138. goto out;
  139. } else if (!di) {
  140. /* Shouldn't happen but just in case... */
  141. btrfs_release_path(path);
  142. goto again;
  143. }
  144. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  145. if (ret)
  146. goto out;
  147. /*
  148. * We have a value to set, so go back and try to insert it now.
  149. */
  150. if (value) {
  151. btrfs_release_path(path);
  152. goto again;
  153. }
  154. }
  155. out:
  156. btrfs_free_path(path);
  157. return ret;
  158. }
  159. /*
  160. * @value: "" makes the attribute to empty, NULL removes it
  161. */
  162. int __btrfs_setxattr(struct btrfs_trans_handle *trans,
  163. struct inode *inode, const char *name,
  164. const void *value, size_t size, int flags)
  165. {
  166. struct btrfs_root *root = BTRFS_I(inode)->root;
  167. int ret;
  168. if (trans)
  169. return do_setxattr(trans, inode, name, value, size, flags);
  170. trans = btrfs_start_transaction(root, 2);
  171. if (IS_ERR(trans))
  172. return PTR_ERR(trans);
  173. ret = do_setxattr(trans, inode, name, value, size, flags);
  174. if (ret)
  175. goto out;
  176. inode->i_ctime = CURRENT_TIME;
  177. ret = btrfs_update_inode(trans, root, inode);
  178. BUG_ON(ret);
  179. out:
  180. btrfs_end_transaction(trans, root);
  181. return ret;
  182. }
  183. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  184. {
  185. struct btrfs_key key, found_key;
  186. struct inode *inode = dentry->d_inode;
  187. struct btrfs_root *root = BTRFS_I(inode)->root;
  188. struct btrfs_path *path;
  189. struct extent_buffer *leaf;
  190. struct btrfs_dir_item *di;
  191. int ret = 0, slot;
  192. size_t total_size = 0, size_left = size;
  193. unsigned long name_ptr;
  194. size_t name_len;
  195. /*
  196. * ok we want all objects associated with this id.
  197. * NOTE: we set key.offset = 0; because we want to start with the
  198. * first xattr that we find and walk forward
  199. */
  200. key.objectid = btrfs_ino(inode);
  201. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  202. key.offset = 0;
  203. path = btrfs_alloc_path();
  204. if (!path)
  205. return -ENOMEM;
  206. path->reada = 2;
  207. /* search for our xattrs */
  208. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  209. if (ret < 0)
  210. goto err;
  211. while (1) {
  212. leaf = path->nodes[0];
  213. slot = path->slots[0];
  214. /* this is where we start walking through the path */
  215. if (slot >= btrfs_header_nritems(leaf)) {
  216. /*
  217. * if we've reached the last slot in this leaf we need
  218. * to go to the next leaf and reset everything
  219. */
  220. ret = btrfs_next_leaf(root, path);
  221. if (ret < 0)
  222. goto err;
  223. else if (ret > 0)
  224. break;
  225. continue;
  226. }
  227. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  228. /* check to make sure this item is what we want */
  229. if (found_key.objectid != key.objectid)
  230. break;
  231. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  232. break;
  233. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  234. if (verify_dir_item(root, leaf, di))
  235. continue;
  236. name_len = btrfs_dir_name_len(leaf, di);
  237. total_size += name_len + 1;
  238. /* we are just looking for how big our buffer needs to be */
  239. if (!size)
  240. goto next;
  241. if (!buffer || (name_len + 1) > size_left) {
  242. ret = -ERANGE;
  243. goto err;
  244. }
  245. name_ptr = (unsigned long)(di + 1);
  246. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  247. buffer[name_len] = '\0';
  248. size_left -= name_len + 1;
  249. buffer += name_len + 1;
  250. next:
  251. path->slots[0]++;
  252. }
  253. ret = total_size;
  254. err:
  255. btrfs_free_path(path);
  256. return ret;
  257. }
  258. /*
  259. * List of handlers for synthetic system.* attributes. All real ondisk
  260. * attributes are handled directly.
  261. */
  262. const struct xattr_handler *btrfs_xattr_handlers[] = {
  263. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  264. &btrfs_xattr_acl_access_handler,
  265. &btrfs_xattr_acl_default_handler,
  266. #endif
  267. NULL,
  268. };
  269. /*
  270. * Check if the attribute is in a supported namespace.
  271. *
  272. * This is applied after the check for the synthetic attributes in the system
  273. * namespace.
  274. */
  275. static int btrfs_is_valid_xattr(const char *name)
  276. {
  277. int len = strlen(name);
  278. int prefixlen = 0;
  279. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  280. XATTR_SECURITY_PREFIX_LEN))
  281. prefixlen = XATTR_SECURITY_PREFIX_LEN;
  282. else if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  283. prefixlen = XATTR_SYSTEM_PREFIX_LEN;
  284. else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
  285. prefixlen = XATTR_TRUSTED_PREFIX_LEN;
  286. else if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
  287. prefixlen = XATTR_USER_PREFIX_LEN;
  288. else
  289. return -EOPNOTSUPP;
  290. /*
  291. * The name cannot consist of just prefix
  292. */
  293. if (len <= prefixlen)
  294. return -EINVAL;
  295. return 0;
  296. }
  297. ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
  298. void *buffer, size_t size)
  299. {
  300. int ret;
  301. /*
  302. * If this is a request for a synthetic attribute in the system.*
  303. * namespace use the generic infrastructure to resolve a handler
  304. * for it via sb->s_xattr.
  305. */
  306. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  307. return generic_getxattr(dentry, name, buffer, size);
  308. ret = btrfs_is_valid_xattr(name);
  309. if (ret)
  310. return ret;
  311. return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
  312. }
  313. int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  314. size_t size, int flags)
  315. {
  316. struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
  317. int ret;
  318. /*
  319. * The permission on security.* and system.* is not checked
  320. * in permission().
  321. */
  322. if (btrfs_root_readonly(root))
  323. return -EROFS;
  324. /*
  325. * If this is a request for a synthetic attribute in the system.*
  326. * namespace use the generic infrastructure to resolve a handler
  327. * for it via sb->s_xattr.
  328. */
  329. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  330. return generic_setxattr(dentry, name, value, size, flags);
  331. ret = btrfs_is_valid_xattr(name);
  332. if (ret)
  333. return ret;
  334. if (size == 0)
  335. value = ""; /* empty EA, do not remove */
  336. return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
  337. flags);
  338. }
  339. int btrfs_removexattr(struct dentry *dentry, const char *name)
  340. {
  341. struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
  342. int ret;
  343. /*
  344. * The permission on security.* and system.* is not checked
  345. * in permission().
  346. */
  347. if (btrfs_root_readonly(root))
  348. return -EROFS;
  349. /*
  350. * If this is a request for a synthetic attribute in the system.*
  351. * namespace use the generic infrastructure to resolve a handler
  352. * for it via sb->s_xattr.
  353. */
  354. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  355. return generic_removexattr(dentry, name);
  356. ret = btrfs_is_valid_xattr(name);
  357. if (ret)
  358. return ret;
  359. return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
  360. XATTR_REPLACE);
  361. }
  362. int btrfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  363. void *fs_info)
  364. {
  365. const struct xattr *xattr;
  366. struct btrfs_trans_handle *trans = fs_info;
  367. char *name;
  368. int err = 0;
  369. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  370. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  371. strlen(xattr->name) + 1, GFP_NOFS);
  372. if (!name) {
  373. err = -ENOMEM;
  374. break;
  375. }
  376. strcpy(name, XATTR_SECURITY_PREFIX);
  377. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  378. err = __btrfs_setxattr(trans, inode, name,
  379. xattr->value, xattr->value_len, 0);
  380. kfree(name);
  381. if (err < 0)
  382. break;
  383. }
  384. return err;
  385. }
  386. int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
  387. struct inode *inode, struct inode *dir,
  388. const struct qstr *qstr)
  389. {
  390. return security_inode_init_security(inode, dir, qstr,
  391. &btrfs_initxattrs, trans);
  392. }