xattr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. mutex_lock(&root->fs_info->fs_mutex);
  136. /* lookup the xattr by name */
  137. di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
  138. strlen(name), 0);
  139. if (!di || IS_ERR(di)) {
  140. ret = -ENODATA;
  141. goto out;
  142. }
  143. leaf = path->nodes[0];
  144. /* if size is 0, that means we want the size of the attr */
  145. if (!size) {
  146. ret = btrfs_dir_data_len(leaf, di);
  147. goto out;
  148. }
  149. /* now get the data out of our dir_item */
  150. if (btrfs_dir_data_len(leaf, di) > size) {
  151. ret = -ERANGE;
  152. goto out;
  153. }
  154. data_ptr = (unsigned long)((char *)(di + 1) +
  155. btrfs_dir_name_len(leaf, di));
  156. read_extent_buffer(leaf, buffer, data_ptr,
  157. btrfs_dir_data_len(leaf, di));
  158. ret = btrfs_dir_data_len(leaf, di);
  159. out:
  160. mutex_unlock(&root->fs_info->fs_mutex);
  161. kfree(name);
  162. btrfs_free_path(path);
  163. return ret;
  164. }
  165. int btrfs_xattr_set(struct inode *inode, int name_index,
  166. const char *attr_name, const void *value, size_t size,
  167. int flags)
  168. {
  169. struct btrfs_dir_item *di;
  170. struct btrfs_root *root = BTRFS_I(inode)->root;
  171. struct btrfs_trans_handle *trans;
  172. struct btrfs_path *path;
  173. struct xattr_handler *handler = btrfs_xattr_handler(name_index);
  174. char *name;
  175. int ret = 0, mod = 0;
  176. if (!handler)
  177. return -EOPNOTSUPP;
  178. name = get_name(attr_name, name_index);
  179. if (!name)
  180. return -ENOMEM;
  181. path = btrfs_alloc_path();
  182. if (!path) {
  183. kfree(name);
  184. return -ENOMEM;
  185. }
  186. mutex_lock(&root->fs_info->fs_mutex);
  187. trans = btrfs_start_transaction(root, 1);
  188. btrfs_set_trans_block_group(trans, inode);
  189. /* first lets see if we already have this xattr */
  190. di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
  191. strlen(name), -1);
  192. if (IS_ERR(di)) {
  193. ret = PTR_ERR(di);
  194. goto out;
  195. }
  196. /* ok we already have this xattr, lets remove it */
  197. if (di) {
  198. /* if we want create only exit */
  199. if (flags & XATTR_CREATE) {
  200. ret = -EEXIST;
  201. goto out;
  202. }
  203. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  204. if (ret)
  205. goto out;
  206. btrfs_release_path(root, path);
  207. /* if we don't have a value then we are removing the xattr */
  208. if (!value) {
  209. mod = 1;
  210. goto out;
  211. }
  212. } else if (flags & XATTR_REPLACE) {
  213. /* we couldn't find the attr to replace, so error out */
  214. ret = -ENODATA;
  215. goto out;
  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. mutex_unlock(&root->fs_info->fs_mutex);
  230. kfree(name);
  231. btrfs_free_path(path);
  232. return ret;
  233. }
  234. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  235. {
  236. struct btrfs_key key, found_key;
  237. struct inode *inode = dentry->d_inode;
  238. struct btrfs_root *root = BTRFS_I(inode)->root;
  239. struct btrfs_path *path;
  240. struct btrfs_item *item;
  241. struct extent_buffer *leaf;
  242. struct btrfs_dir_item *di;
  243. struct xattr_handler *handler;
  244. int ret = 0, slot, advance;
  245. size_t total_size = 0, size_left = size, written;
  246. unsigned long name_ptr;
  247. char *name;
  248. u32 nritems;
  249. /*
  250. * ok we want all objects associated with this id.
  251. * NOTE: we set key.offset = 0; because we want to start with the
  252. * first xattr that we find and walk forward
  253. */
  254. key.objectid = inode->i_ino;
  255. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  256. key.offset = 0;
  257. path = btrfs_alloc_path();
  258. if (!path)
  259. return -ENOMEM;
  260. path->reada = 2;
  261. mutex_lock(&root->fs_info->fs_mutex);
  262. /* search for our xattrs */
  263. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  264. if (ret < 0)
  265. goto err;
  266. ret = 0;
  267. advance = 0;
  268. while (1) {
  269. leaf = path->nodes[0];
  270. nritems = btrfs_header_nritems(leaf);
  271. slot = path->slots[0];
  272. /* this is where we start walking through the path */
  273. if (advance || slot >= nritems) {
  274. /*
  275. * if we've reached the last slot in this leaf we need
  276. * to go to the next leaf and reset everything
  277. */
  278. if (slot >= nritems-1) {
  279. ret = btrfs_next_leaf(root, path);
  280. if (ret)
  281. break;
  282. leaf = path->nodes[0];
  283. nritems = btrfs_header_nritems(leaf);
  284. slot = path->slots[0];
  285. } else {
  286. /*
  287. * just walking through the slots on this leaf
  288. */
  289. slot++;
  290. path->slots[0]++;
  291. }
  292. }
  293. advance = 1;
  294. item = btrfs_item_nr(leaf, slot);
  295. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  296. /* check to make sure this item is what we want */
  297. if (found_key.objectid != key.objectid)
  298. break;
  299. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  300. break;
  301. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  302. total_size += btrfs_dir_name_len(leaf, di)+1;
  303. /* we are just looking for how big our buffer needs to be */
  304. if (!size)
  305. continue;
  306. /* find our handler for this xattr */
  307. name_ptr = (unsigned long)(di + 1);
  308. handler = find_btrfs_xattr_handler(leaf, name_ptr,
  309. btrfs_dir_name_len(leaf, di));
  310. if (!handler) {
  311. printk(KERN_ERR "btrfs: unsupported xattr found\n");
  312. continue;
  313. }
  314. name = kmalloc(btrfs_dir_name_len(leaf, di), GFP_KERNEL);
  315. read_extent_buffer(leaf, name, name_ptr,
  316. btrfs_dir_name_len(leaf, di));
  317. /* call the list function associated with this xattr */
  318. written = handler->list(inode, buffer, size_left, name,
  319. btrfs_dir_name_len(leaf, di));
  320. kfree(name);
  321. if (written < 0) {
  322. ret = -ERANGE;
  323. break;
  324. }
  325. size_left -= written;
  326. buffer += written;
  327. }
  328. ret = total_size;
  329. err:
  330. mutex_unlock(&root->fs_info->fs_mutex);
  331. btrfs_free_path(path);
  332. return ret;
  333. }
  334. /*
  335. * delete all the xattrs associated with the inode. fs_mutex should be
  336. * held when we come into here
  337. */
  338. int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
  339. struct btrfs_root *root, struct inode *inode)
  340. {
  341. struct btrfs_path *path;
  342. struct btrfs_key key, found_key;
  343. struct btrfs_item *item;
  344. struct extent_buffer *leaf;
  345. int ret;
  346. path = btrfs_alloc_path();
  347. if (!path)
  348. return -ENOMEM;
  349. path->reada = -1;
  350. key.objectid = inode->i_ino;
  351. btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
  352. key.offset = (u64)-1;
  353. while(1) {
  354. /* look for our next xattr */
  355. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  356. if (ret < 0)
  357. goto out;
  358. BUG_ON(ret == 0);
  359. if (path->slots[0] == 0)
  360. break;
  361. path->slots[0]--;
  362. leaf = path->nodes[0];
  363. item = btrfs_item_nr(leaf, path->slots[0]);
  364. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  365. if (found_key.objectid != key.objectid)
  366. break;
  367. if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
  368. break;
  369. ret = btrfs_del_item(trans, root, path);
  370. BUG_ON(ret);
  371. btrfs_release_path(root, path);
  372. }
  373. ret = 0;
  374. out:
  375. btrfs_free_path(path);
  376. return ret;
  377. }
  378. /*
  379. * Handler functions
  380. */
  381. #define BTRFS_XATTR_SETGET_FUNCS(name, index) \
  382. static int btrfs_xattr_##name##_get(struct inode *inode, \
  383. const char *name, void *value, \
  384. size_t size) \
  385. { \
  386. if (*name == '\0') \
  387. return -EINVAL; \
  388. return btrfs_xattr_get(inode, index, name, value, size); \
  389. } \
  390. static int btrfs_xattr_##name##_set(struct inode *inode, \
  391. const char *name, const void *value,\
  392. size_t size, int flags) \
  393. { \
  394. if (*name == '\0') \
  395. return -EINVAL; \
  396. return btrfs_xattr_set(inode, index, name, value, size, flags); \
  397. }
  398. BTRFS_XATTR_SETGET_FUNCS(security, BTRFS_XATTR_INDEX_SECURITY);
  399. BTRFS_XATTR_SETGET_FUNCS(system, BTRFS_XATTR_INDEX_SYSTEM);
  400. BTRFS_XATTR_SETGET_FUNCS(user, BTRFS_XATTR_INDEX_USER);
  401. BTRFS_XATTR_SETGET_FUNCS(trusted, BTRFS_XATTR_INDEX_TRUSTED);
  402. struct xattr_handler btrfs_xattr_security_handler = {
  403. .prefix = XATTR_SECURITY_PREFIX,
  404. .list = btrfs_xattr_generic_list,
  405. .get = btrfs_xattr_security_get,
  406. .set = btrfs_xattr_security_set,
  407. };
  408. struct xattr_handler btrfs_xattr_system_handler = {
  409. .prefix = XATTR_SYSTEM_PREFIX,
  410. .list = btrfs_xattr_generic_list,
  411. .get = btrfs_xattr_system_get,
  412. .set = btrfs_xattr_system_set,
  413. };
  414. struct xattr_handler btrfs_xattr_user_handler = {
  415. .prefix = XATTR_USER_PREFIX,
  416. .list = btrfs_xattr_generic_list,
  417. .get = btrfs_xattr_user_get,
  418. .set = btrfs_xattr_user_set,
  419. };
  420. struct xattr_handler btrfs_xattr_trusted_handler = {
  421. .prefix = XATTR_TRUSTED_PREFIX,
  422. .list = btrfs_xattr_generic_list,
  423. .get = btrfs_xattr_trusted_get,
  424. .set = btrfs_xattr_trusted_set,
  425. };