xattr.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "ctree.h"
  24. #include "btrfs_inode.h"
  25. #include "transaction.h"
  26. #include "xattr.h"
  27. #include "disk-io.h"
  28. static struct xattr_handler *btrfs_xattr_handler_map[] = {
  29. [BTRFS_XATTR_INDEX_USER] = &btrfs_xattr_user_handler,
  30. #ifdef CONFIG_FS_POSIX_ACL
  31. [BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &btrfs_xattr_acl_access_handler,
  32. [BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &btrfs_xattr_acl_default_handler,
  33. #endif
  34. [BTRFS_XATTR_INDEX_TRUSTED] = &btrfs_xattr_trusted_handler,
  35. [BTRFS_XATTR_INDEX_SECURITY] = &btrfs_xattr_security_handler,
  36. [BTRFS_XATTR_INDEX_SYSTEM] = &btrfs_xattr_system_handler,
  37. };
  38. struct xattr_handler *btrfs_xattr_handlers[] = {
  39. &btrfs_xattr_user_handler,
  40. #ifdef CONFIG_FS_POSIX_ACL
  41. &btrfs_xattr_acl_access_handler,
  42. &btrfs_xattr_acl_default_handler,
  43. #endif
  44. &btrfs_xattr_trusted_handler,
  45. &btrfs_xattr_security_handler,
  46. &btrfs_xattr_system_handler,
  47. NULL,
  48. };
  49. /*
  50. * @param name_index - the index for the xattr handler
  51. * @return the xattr_handler if we found it, NULL otherwise
  52. *
  53. * use this if we know the type of the xattr already
  54. */
  55. static struct xattr_handler *btrfs_xattr_handler(int name_index)
  56. {
  57. struct xattr_handler *handler = NULL;
  58. if (name_index >= 0 &&
  59. name_index < ARRAY_SIZE(btrfs_xattr_handler_map))
  60. handler = btrfs_xattr_handler_map[name_index];
  61. return handler;
  62. }
  63. static inline char *get_name(const char *name, int name_index)
  64. {
  65. char *ret = NULL;
  66. struct xattr_handler *handler = btrfs_xattr_handler(name_index);
  67. int prefix_len;
  68. if (!handler)
  69. return ret;
  70. prefix_len = strlen(handler->prefix);
  71. ret = kmalloc(strlen(name) + prefix_len + 1, GFP_KERNEL);
  72. if (!ret)
  73. return ret;
  74. memcpy(ret, handler->prefix, prefix_len);
  75. memcpy(ret+prefix_len, name, strlen(name));
  76. ret[prefix_len + strlen(name)] = '\0';
  77. return ret;
  78. }
  79. ssize_t btrfs_xattr_get(struct inode *inode, int name_index,
  80. const char *attr_name, void *buffer, size_t size)
  81. {
  82. struct btrfs_dir_item *di;
  83. struct btrfs_root *root = BTRFS_I(inode)->root;
  84. struct btrfs_path *path;
  85. struct extent_buffer *leaf;
  86. struct xattr_handler *handler = btrfs_xattr_handler(name_index);
  87. int ret = 0;
  88. unsigned long data_ptr;
  89. char *name;
  90. if (!handler)
  91. return -EOPNOTSUPP;
  92. name = get_name(attr_name, name_index);
  93. if (!name)
  94. return -ENOMEM;
  95. path = btrfs_alloc_path();
  96. if (!path) {
  97. kfree(name);
  98. return -ENOMEM;
  99. }
  100. /* lookup the xattr by name */
  101. di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
  102. strlen(name), 0);
  103. if (!di || IS_ERR(di)) {
  104. ret = -ENODATA;
  105. goto out;
  106. }
  107. leaf = path->nodes[0];
  108. /* if size is 0, that means we want the size of the attr */
  109. if (!size) {
  110. ret = btrfs_dir_data_len(leaf, di);
  111. goto out;
  112. }
  113. /* now get the data out of our dir_item */
  114. if (btrfs_dir_data_len(leaf, di) > size) {
  115. ret = -ERANGE;
  116. goto out;
  117. }
  118. data_ptr = (unsigned long)((char *)(di + 1) +
  119. btrfs_dir_name_len(leaf, di));
  120. read_extent_buffer(leaf, buffer, data_ptr,
  121. btrfs_dir_data_len(leaf, di));
  122. ret = btrfs_dir_data_len(leaf, di);
  123. out:
  124. kfree(name);
  125. btrfs_free_path(path);
  126. return ret;
  127. }
  128. int btrfs_xattr_set(struct inode *inode, int name_index,
  129. const char *attr_name, const void *value, size_t size,
  130. int flags)
  131. {
  132. struct btrfs_dir_item *di;
  133. struct btrfs_root *root = BTRFS_I(inode)->root;
  134. struct btrfs_trans_handle *trans;
  135. struct btrfs_path *path;
  136. struct xattr_handler *handler = btrfs_xattr_handler(name_index);
  137. char *name;
  138. int ret = 0, mod = 0;
  139. if (!handler)
  140. return -EOPNOTSUPP;
  141. name = get_name(attr_name, name_index);
  142. if (!name)
  143. return -ENOMEM;
  144. path = btrfs_alloc_path();
  145. if (!path) {
  146. kfree(name);
  147. return -ENOMEM;
  148. }
  149. trans = btrfs_start_transaction(root, 1);
  150. btrfs_set_trans_block_group(trans, inode);
  151. /* first lets see if we already have this xattr */
  152. di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
  153. strlen(name), -1);
  154. if (IS_ERR(di)) {
  155. ret = PTR_ERR(di);
  156. goto out;
  157. }
  158. /* ok we already have this xattr, lets remove it */
  159. if (di) {
  160. /* if we want create only exit */
  161. if (flags & XATTR_CREATE) {
  162. ret = -EEXIST;
  163. goto out;
  164. }
  165. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  166. if (ret)
  167. goto out;
  168. btrfs_release_path(root, path);
  169. /* if we don't have a value then we are removing the xattr */
  170. if (!value) {
  171. mod = 1;
  172. goto out;
  173. }
  174. } else {
  175. btrfs_release_path(root, path);
  176. if (flags & XATTR_REPLACE) {
  177. /* we couldn't find the attr to replace */
  178. ret = -ENODATA;
  179. goto out;
  180. }
  181. }
  182. /* ok we have to create a completely new xattr */
  183. ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
  184. value, size, inode->i_ino);
  185. if (ret)
  186. goto out;
  187. mod = 1;
  188. out:
  189. if (mod) {
  190. inode->i_ctime = CURRENT_TIME;
  191. ret = btrfs_update_inode(trans, root, inode);
  192. }
  193. btrfs_end_transaction(trans, root);
  194. kfree(name);
  195. btrfs_free_path(path);
  196. return ret;
  197. }
  198. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  199. {
  200. struct btrfs_key key, found_key;
  201. struct inode *inode = dentry->d_inode;
  202. struct btrfs_root *root = BTRFS_I(inode)->root;
  203. struct btrfs_path *path;
  204. struct btrfs_item *item;
  205. struct extent_buffer *leaf;
  206. struct btrfs_dir_item *di;
  207. int ret = 0, slot, advance;
  208. size_t total_size = 0, size_left = size;
  209. unsigned long name_ptr;
  210. size_t name_len;
  211. u32 nritems;
  212. /*
  213. * ok we want all objects associated with this id.
  214. * NOTE: we set key.offset = 0; because we want to start with the
  215. * first xattr that we find and walk forward
  216. */
  217. key.objectid = inode->i_ino;
  218. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  219. key.offset = 0;
  220. path = btrfs_alloc_path();
  221. if (!path)
  222. return -ENOMEM;
  223. path->reada = 2;
  224. /* search for our xattrs */
  225. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  226. if (ret < 0)
  227. goto err;
  228. ret = 0;
  229. advance = 0;
  230. while (1) {
  231. leaf = path->nodes[0];
  232. nritems = btrfs_header_nritems(leaf);
  233. slot = path->slots[0];
  234. /* this is where we start walking through the path */
  235. if (advance || slot >= nritems) {
  236. /*
  237. * if we've reached the last slot in this leaf we need
  238. * to go to the next leaf and reset everything
  239. */
  240. if (slot >= nritems-1) {
  241. ret = btrfs_next_leaf(root, path);
  242. if (ret)
  243. break;
  244. leaf = path->nodes[0];
  245. nritems = btrfs_header_nritems(leaf);
  246. slot = path->slots[0];
  247. } else {
  248. /*
  249. * just walking through the slots on this leaf
  250. */
  251. slot++;
  252. path->slots[0]++;
  253. }
  254. }
  255. advance = 1;
  256. item = btrfs_item_nr(leaf, slot);
  257. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  258. /* check to make sure this item is what we want */
  259. if (found_key.objectid != key.objectid)
  260. break;
  261. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  262. break;
  263. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  264. name_len = btrfs_dir_name_len(leaf, di);
  265. total_size += name_len + 1;
  266. /* we are just looking for how big our buffer needs to be */
  267. if (!size)
  268. continue;
  269. if (!buffer || (name_len + 1) > size_left) {
  270. ret = -ERANGE;
  271. break;
  272. }
  273. name_ptr = (unsigned long)(di + 1);
  274. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  275. buffer[name_len] = '\0';
  276. size_left -= name_len + 1;
  277. buffer += name_len + 1;
  278. }
  279. ret = total_size;
  280. err:
  281. btrfs_free_path(path);
  282. return ret;
  283. }
  284. /*
  285. * Handler functions
  286. */
  287. #define BTRFS_XATTR_SETGET_FUNCS(name, index) \
  288. static int btrfs_xattr_##name##_get(struct inode *inode, \
  289. const char *name, void *value, \
  290. size_t size) \
  291. { \
  292. if (*name == '\0') \
  293. return -EINVAL; \
  294. return btrfs_xattr_get(inode, index, name, value, size); \
  295. } \
  296. static int btrfs_xattr_##name##_set(struct inode *inode, \
  297. const char *name, const void *value,\
  298. size_t size, int flags) \
  299. { \
  300. if (*name == '\0') \
  301. return -EINVAL; \
  302. return btrfs_xattr_set(inode, index, name, value, size, flags); \
  303. }
  304. BTRFS_XATTR_SETGET_FUNCS(security, BTRFS_XATTR_INDEX_SECURITY);
  305. BTRFS_XATTR_SETGET_FUNCS(system, BTRFS_XATTR_INDEX_SYSTEM);
  306. BTRFS_XATTR_SETGET_FUNCS(user, BTRFS_XATTR_INDEX_USER);
  307. BTRFS_XATTR_SETGET_FUNCS(trusted, BTRFS_XATTR_INDEX_TRUSTED);
  308. struct xattr_handler btrfs_xattr_security_handler = {
  309. .prefix = XATTR_SECURITY_PREFIX,
  310. .get = btrfs_xattr_security_get,
  311. .set = btrfs_xattr_security_set,
  312. };
  313. struct xattr_handler btrfs_xattr_system_handler = {
  314. .prefix = XATTR_SYSTEM_PREFIX,
  315. .get = btrfs_xattr_system_get,
  316. .set = btrfs_xattr_system_set,
  317. };
  318. struct xattr_handler btrfs_xattr_user_handler = {
  319. .prefix = XATTR_USER_PREFIX,
  320. .get = btrfs_xattr_user_get,
  321. .set = btrfs_xattr_user_set,
  322. };
  323. struct xattr_handler btrfs_xattr_trusted_handler = {
  324. .prefix = XATTR_TRUSTED_PREFIX,
  325. .get = btrfs_xattr_trusted_get,
  326. .set = btrfs_xattr_trusted_set,
  327. };