xattr.c 11 KB

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