xattr.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. int __btrfs_setxattr(struct inode *inode, const char *name,
  79. const void *value, size_t size, int flags)
  80. {
  81. struct btrfs_dir_item *di;
  82. struct btrfs_root *root = BTRFS_I(inode)->root;
  83. struct btrfs_trans_handle *trans;
  84. struct btrfs_path *path;
  85. int ret = 0, mod = 0;
  86. path = btrfs_alloc_path();
  87. if (!path)
  88. return -ENOMEM;
  89. trans = btrfs_join_transaction(root, 1);
  90. btrfs_set_trans_block_group(trans, inode);
  91. /* first lets see if we already have this xattr */
  92. di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
  93. strlen(name), -1);
  94. if (IS_ERR(di)) {
  95. ret = PTR_ERR(di);
  96. goto out;
  97. }
  98. /* ok we already have this xattr, lets remove it */
  99. if (di) {
  100. /* if we want create only exit */
  101. if (flags & XATTR_CREATE) {
  102. ret = -EEXIST;
  103. goto out;
  104. }
  105. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  106. if (ret)
  107. goto out;
  108. btrfs_release_path(root, path);
  109. /* if we don't have a value then we are removing the xattr */
  110. if (!value) {
  111. mod = 1;
  112. goto out;
  113. }
  114. } else {
  115. btrfs_release_path(root, path);
  116. if (flags & XATTR_REPLACE) {
  117. /* we couldn't find the attr to replace */
  118. ret = -ENODATA;
  119. goto out;
  120. }
  121. }
  122. /* ok we have to create a completely new xattr */
  123. ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
  124. value, size, inode->i_ino);
  125. if (ret)
  126. goto out;
  127. mod = 1;
  128. out:
  129. if (mod) {
  130. inode->i_ctime = CURRENT_TIME;
  131. ret = btrfs_update_inode(trans, root, inode);
  132. }
  133. btrfs_end_transaction(trans, root);
  134. btrfs_free_path(path);
  135. return ret;
  136. }
  137. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  138. {
  139. struct btrfs_key key, found_key;
  140. struct inode *inode = dentry->d_inode;
  141. struct btrfs_root *root = BTRFS_I(inode)->root;
  142. struct btrfs_path *path;
  143. struct btrfs_item *item;
  144. struct extent_buffer *leaf;
  145. struct btrfs_dir_item *di;
  146. int ret = 0, slot, advance;
  147. size_t total_size = 0, size_left = size;
  148. unsigned long name_ptr;
  149. size_t name_len;
  150. u32 nritems;
  151. /*
  152. * ok we want all objects associated with this id.
  153. * NOTE: we set key.offset = 0; because we want to start with the
  154. * first xattr that we find and walk forward
  155. */
  156. key.objectid = inode->i_ino;
  157. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  158. key.offset = 0;
  159. path = btrfs_alloc_path();
  160. if (!path)
  161. return -ENOMEM;
  162. path->reada = 2;
  163. /* search for our xattrs */
  164. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  165. if (ret < 0)
  166. goto err;
  167. advance = 0;
  168. while (1) {
  169. leaf = path->nodes[0];
  170. nritems = btrfs_header_nritems(leaf);
  171. slot = path->slots[0];
  172. /* this is where we start walking through the path */
  173. if (advance || slot >= nritems) {
  174. /*
  175. * if we've reached the last slot in this leaf we need
  176. * to go to the next leaf and reset everything
  177. */
  178. if (slot >= nritems-1) {
  179. ret = btrfs_next_leaf(root, path);
  180. if (ret)
  181. break;
  182. leaf = path->nodes[0];
  183. nritems = btrfs_header_nritems(leaf);
  184. slot = path->slots[0];
  185. } else {
  186. /*
  187. * just walking through the slots on this leaf
  188. */
  189. slot++;
  190. path->slots[0]++;
  191. }
  192. }
  193. advance = 1;
  194. item = btrfs_item_nr(leaf, slot);
  195. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  196. /* check to make sure this item is what we want */
  197. if (found_key.objectid != key.objectid)
  198. break;
  199. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  200. break;
  201. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  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. continue;
  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. }
  217. ret = total_size;
  218. err:
  219. btrfs_free_path(path);
  220. return ret;
  221. }
  222. /*
  223. * List of handlers for synthetic system.* attributes. All real ondisk
  224. * attributes are handled directly.
  225. */
  226. struct xattr_handler *btrfs_xattr_handlers[] = {
  227. #ifdef CONFIG_FS_POSIX_ACL
  228. &btrfs_xattr_acl_access_handler,
  229. &btrfs_xattr_acl_default_handler,
  230. #endif
  231. NULL,
  232. };
  233. /*
  234. * Check if the attribute is in a supported namespace.
  235. *
  236. * This applied after the check for the synthetic attributes in the system
  237. * namespace.
  238. */
  239. static bool btrfs_is_valid_xattr(const char *name)
  240. {
  241. return !strncmp(name, XATTR_SECURITY_PREFIX,
  242. XATTR_SECURITY_PREFIX_LEN) ||
  243. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
  244. !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  245. !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  246. }
  247. ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
  248. void *buffer, size_t size)
  249. {
  250. /*
  251. * If this is a request for a synthetic attribute in the system.*
  252. * namespace use the generic infrastructure to resolve a handler
  253. * for it via sb->s_xattr.
  254. */
  255. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  256. return generic_getxattr(dentry, name, buffer, size);
  257. if (!btrfs_is_valid_xattr(name))
  258. return -EOPNOTSUPP;
  259. return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
  260. }
  261. int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  262. size_t size, int flags)
  263. {
  264. /*
  265. * If this is a request for a synthetic attribute in the system.*
  266. * namespace use the generic infrastructure to resolve a handler
  267. * for it via sb->s_xattr.
  268. */
  269. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  270. return generic_setxattr(dentry, name, value, size, flags);
  271. if (!btrfs_is_valid_xattr(name))
  272. return -EOPNOTSUPP;
  273. if (size == 0)
  274. value = ""; /* empty EA, do not remove */
  275. return __btrfs_setxattr(dentry->d_inode, name, value, size, flags);
  276. }
  277. int btrfs_removexattr(struct dentry *dentry, const char *name)
  278. {
  279. /*
  280. * If this is a request for a synthetic attribute in the system.*
  281. * namespace use the generic infrastructure to resolve a handler
  282. * for it via sb->s_xattr.
  283. */
  284. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  285. return generic_removexattr(dentry, name);
  286. if (!btrfs_is_valid_xattr(name))
  287. return -EOPNOTSUPP;
  288. return __btrfs_setxattr(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
  289. }
  290. int btrfs_xattr_security_init(struct inode *inode, struct inode *dir)
  291. {
  292. int err;
  293. size_t len;
  294. void *value;
  295. char *suffix;
  296. char *name;
  297. err = security_inode_init_security(inode, dir, &suffix, &value, &len);
  298. if (err) {
  299. if (err == -EOPNOTSUPP)
  300. return 0;
  301. return err;
  302. }
  303. name = kmalloc(XATTR_SECURITY_PREFIX_LEN + strlen(suffix) + 1,
  304. GFP_NOFS);
  305. if (!name) {
  306. err = -ENOMEM;
  307. } else {
  308. strcpy(name, XATTR_SECURITY_PREFIX);
  309. strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
  310. err = __btrfs_setxattr(inode, name, value, len, 0);
  311. kfree(name);
  312. }
  313. kfree(suffix);
  314. kfree(value);
  315. return err;
  316. }