xattr.c 11 KB

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