xattr.c 11 KB

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