xattr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 if (flags & XATTR_REPLACE) {
  210. /* we couldn't find the attr to replace, so error out */
  211. ret = -ENODATA;
  212. goto out;
  213. }
  214. /* ok we have to create a completely new xattr */
  215. ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
  216. value, size, inode->i_ino);
  217. if (ret)
  218. goto out;
  219. mod = 1;
  220. out:
  221. if (mod) {
  222. inode->i_ctime = CURRENT_TIME;
  223. ret = btrfs_update_inode(trans, root, inode);
  224. }
  225. btrfs_end_transaction(trans, root);
  226. kfree(name);
  227. btrfs_free_path(path);
  228. return ret;
  229. }
  230. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  231. {
  232. struct btrfs_key key, found_key;
  233. struct inode *inode = dentry->d_inode;
  234. struct btrfs_root *root = BTRFS_I(inode)->root;
  235. struct btrfs_path *path;
  236. struct btrfs_item *item;
  237. struct extent_buffer *leaf;
  238. struct btrfs_dir_item *di;
  239. struct xattr_handler *handler;
  240. int ret = 0, slot, advance;
  241. size_t total_size = 0, size_left = size, written;
  242. unsigned long name_ptr;
  243. char *name;
  244. u32 nritems;
  245. /*
  246. * ok we want all objects associated with this id.
  247. * NOTE: we set key.offset = 0; because we want to start with the
  248. * first xattr that we find and walk forward
  249. */
  250. key.objectid = inode->i_ino;
  251. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  252. key.offset = 0;
  253. path = btrfs_alloc_path();
  254. if (!path)
  255. return -ENOMEM;
  256. path->reada = 2;
  257. /* search for our xattrs */
  258. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  259. if (ret < 0)
  260. goto err;
  261. ret = 0;
  262. advance = 0;
  263. while (1) {
  264. leaf = path->nodes[0];
  265. nritems = btrfs_header_nritems(leaf);
  266. slot = path->slots[0];
  267. /* this is where we start walking through the path */
  268. if (advance || slot >= nritems) {
  269. /*
  270. * if we've reached the last slot in this leaf we need
  271. * to go to the next leaf and reset everything
  272. */
  273. if (slot >= nritems-1) {
  274. ret = btrfs_next_leaf(root, path);
  275. if (ret)
  276. break;
  277. leaf = path->nodes[0];
  278. nritems = btrfs_header_nritems(leaf);
  279. slot = path->slots[0];
  280. } else {
  281. /*
  282. * just walking through the slots on this leaf
  283. */
  284. slot++;
  285. path->slots[0]++;
  286. }
  287. }
  288. advance = 1;
  289. item = btrfs_item_nr(leaf, slot);
  290. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  291. /* check to make sure this item is what we want */
  292. if (found_key.objectid != key.objectid)
  293. break;
  294. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  295. break;
  296. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  297. total_size += btrfs_dir_name_len(leaf, di)+1;
  298. /* we are just looking for how big our buffer needs to be */
  299. if (!size)
  300. continue;
  301. /* find our handler for this xattr */
  302. name_ptr = (unsigned long)(di + 1);
  303. handler = find_btrfs_xattr_handler(leaf, name_ptr,
  304. btrfs_dir_name_len(leaf, di));
  305. if (!handler) {
  306. printk(KERN_ERR "btrfs: unsupported xattr found\n");
  307. continue;
  308. }
  309. name = kmalloc(btrfs_dir_name_len(leaf, di), GFP_KERNEL);
  310. read_extent_buffer(leaf, name, name_ptr,
  311. btrfs_dir_name_len(leaf, di));
  312. /* call the list function associated with this xattr */
  313. written = handler->list(inode, buffer, size_left, name,
  314. btrfs_dir_name_len(leaf, di));
  315. kfree(name);
  316. if (written < 0) {
  317. ret = -ERANGE;
  318. break;
  319. }
  320. size_left -= written;
  321. buffer += written;
  322. }
  323. ret = total_size;
  324. err:
  325. btrfs_free_path(path);
  326. return ret;
  327. }
  328. /*
  329. * delete all the xattrs associated with the inode.
  330. */
  331. int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
  332. struct btrfs_root *root, struct inode *inode)
  333. {
  334. struct btrfs_path *path;
  335. struct btrfs_key key, found_key;
  336. struct btrfs_item *item;
  337. struct extent_buffer *leaf;
  338. int ret;
  339. path = btrfs_alloc_path();
  340. if (!path)
  341. return -ENOMEM;
  342. path->reada = -1;
  343. key.objectid = inode->i_ino;
  344. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  345. key.offset = (u64)-1;
  346. while(1) {
  347. /* look for our next xattr */
  348. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  349. if (ret < 0)
  350. goto out;
  351. BUG_ON(ret == 0);
  352. if (path->slots[0] == 0)
  353. break;
  354. path->slots[0]--;
  355. leaf = path->nodes[0];
  356. item = btrfs_item_nr(leaf, path->slots[0]);
  357. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  358. if (found_key.objectid != key.objectid)
  359. break;
  360. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  361. break;
  362. ret = btrfs_del_item(trans, root, path);
  363. BUG_ON(ret);
  364. btrfs_release_path(root, path);
  365. }
  366. ret = 0;
  367. out:
  368. btrfs_free_path(path);
  369. return ret;
  370. }
  371. /*
  372. * Handler functions
  373. */
  374. #define BTRFS_XATTR_SETGET_FUNCS(name, index) \
  375. static int btrfs_xattr_##name##_get(struct inode *inode, \
  376. const char *name, void *value, \
  377. size_t size) \
  378. { \
  379. if (*name == '\0') \
  380. return -EINVAL; \
  381. return btrfs_xattr_get(inode, index, name, value, size); \
  382. } \
  383. static int btrfs_xattr_##name##_set(struct inode *inode, \
  384. const char *name, const void *value,\
  385. size_t size, int flags) \
  386. { \
  387. if (*name == '\0') \
  388. return -EINVAL; \
  389. return btrfs_xattr_set(inode, index, name, value, size, flags); \
  390. }
  391. BTRFS_XATTR_SETGET_FUNCS(security, BTRFS_XATTR_INDEX_SECURITY);
  392. BTRFS_XATTR_SETGET_FUNCS(system, BTRFS_XATTR_INDEX_SYSTEM);
  393. BTRFS_XATTR_SETGET_FUNCS(user, BTRFS_XATTR_INDEX_USER);
  394. BTRFS_XATTR_SETGET_FUNCS(trusted, BTRFS_XATTR_INDEX_TRUSTED);
  395. struct xattr_handler btrfs_xattr_security_handler = {
  396. .prefix = XATTR_SECURITY_PREFIX,
  397. .list = btrfs_xattr_generic_list,
  398. .get = btrfs_xattr_security_get,
  399. .set = btrfs_xattr_security_set,
  400. };
  401. struct xattr_handler btrfs_xattr_system_handler = {
  402. .prefix = XATTR_SYSTEM_PREFIX,
  403. .list = btrfs_xattr_generic_list,
  404. .get = btrfs_xattr_system_get,
  405. .set = btrfs_xattr_system_set,
  406. };
  407. struct xattr_handler btrfs_xattr_user_handler = {
  408. .prefix = XATTR_USER_PREFIX,
  409. .list = btrfs_xattr_generic_list,
  410. .get = btrfs_xattr_user_get,
  411. .set = btrfs_xattr_user_set,
  412. };
  413. struct xattr_handler btrfs_xattr_trusted_handler = {
  414. .prefix = XATTR_TRUSTED_PREFIX,
  415. .list = btrfs_xattr_generic_list,
  416. .get = btrfs_xattr_trusted_get,
  417. .set = btrfs_xattr_trusted_set,
  418. };