xattr.c 12 KB

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