xattr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <linux/security.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include "ctree.h"
  26. #include "btrfs_inode.h"
  27. #include "transaction.h"
  28. #include "xattr.h"
  29. #include "disk-io.h"
  30. #include "props.h"
  31. #include "locking.h"
  32. ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
  33. void *buffer, size_t size)
  34. {
  35. struct btrfs_dir_item *di;
  36. struct btrfs_root *root = BTRFS_I(inode)->root;
  37. struct btrfs_path *path;
  38. struct extent_buffer *leaf;
  39. int ret = 0;
  40. unsigned long data_ptr;
  41. path = btrfs_alloc_path();
  42. if (!path)
  43. return -ENOMEM;
  44. /* lookup the xattr by name */
  45. di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), name,
  46. strlen(name), 0);
  47. if (!di) {
  48. ret = -ENODATA;
  49. goto out;
  50. } else if (IS_ERR(di)) {
  51. ret = PTR_ERR(di);
  52. goto out;
  53. }
  54. leaf = path->nodes[0];
  55. /* if size is 0, that means we want the size of the attr */
  56. if (!size) {
  57. ret = btrfs_dir_data_len(leaf, di);
  58. goto out;
  59. }
  60. /* now get the data out of our dir_item */
  61. if (btrfs_dir_data_len(leaf, di) > size) {
  62. ret = -ERANGE;
  63. goto out;
  64. }
  65. /*
  66. * The way things are packed into the leaf is like this
  67. * |struct btrfs_dir_item|name|data|
  68. * where name is the xattr name, so security.foo, and data is the
  69. * content of the xattr. data_ptr points to the location in memory
  70. * where the data starts in the in memory leaf
  71. */
  72. data_ptr = (unsigned long)((char *)(di + 1) +
  73. btrfs_dir_name_len(leaf, di));
  74. read_extent_buffer(leaf, buffer, data_ptr,
  75. btrfs_dir_data_len(leaf, di));
  76. ret = btrfs_dir_data_len(leaf, di);
  77. out:
  78. btrfs_free_path(path);
  79. return ret;
  80. }
  81. static int do_setxattr(struct btrfs_trans_handle *trans,
  82. struct inode *inode, const char *name,
  83. const void *value, size_t size, int flags)
  84. {
  85. struct btrfs_dir_item *di = NULL;
  86. struct btrfs_root *root = BTRFS_I(inode)->root;
  87. struct btrfs_path *path;
  88. size_t name_len = strlen(name);
  89. int ret = 0;
  90. if (name_len + size > BTRFS_MAX_XATTR_SIZE(root))
  91. return -ENOSPC;
  92. path = btrfs_alloc_path();
  93. if (!path)
  94. return -ENOMEM;
  95. path->skip_release_on_error = 1;
  96. if (!value) {
  97. di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode),
  98. name, name_len, -1);
  99. if (!di && (flags & XATTR_REPLACE))
  100. ret = -ENODATA;
  101. else if (IS_ERR(di))
  102. ret = PTR_ERR(di);
  103. else if (di)
  104. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  105. goto out;
  106. }
  107. /*
  108. * For a replace we can't just do the insert blindly.
  109. * Do a lookup first (read-only btrfs_search_slot), and return if xattr
  110. * doesn't exist. If it exists, fall down below to the insert/replace
  111. * path - we can't race with a concurrent xattr delete, because the VFS
  112. * locks the inode's i_mutex before calling setxattr or removexattr.
  113. */
  114. if (flags & XATTR_REPLACE) {
  115. ASSERT(inode_is_locked(inode));
  116. di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode),
  117. name, name_len, 0);
  118. if (!di)
  119. ret = -ENODATA;
  120. else if (IS_ERR(di))
  121. ret = PTR_ERR(di);
  122. if (ret)
  123. goto out;
  124. btrfs_release_path(path);
  125. di = NULL;
  126. }
  127. ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
  128. name, name_len, value, size);
  129. if (ret == -EOVERFLOW) {
  130. /*
  131. * We have an existing item in a leaf, split_leaf couldn't
  132. * expand it. That item might have or not a dir_item that
  133. * matches our target xattr, so lets check.
  134. */
  135. ret = 0;
  136. btrfs_assert_tree_locked(path->nodes[0]);
  137. di = btrfs_match_dir_item_name(root, path, name, name_len);
  138. if (!di && !(flags & XATTR_REPLACE)) {
  139. ret = -ENOSPC;
  140. goto out;
  141. }
  142. } else if (ret == -EEXIST) {
  143. ret = 0;
  144. di = btrfs_match_dir_item_name(root, path, name, name_len);
  145. ASSERT(di); /* logic error */
  146. } else if (ret) {
  147. goto out;
  148. }
  149. if (di && (flags & XATTR_CREATE)) {
  150. ret = -EEXIST;
  151. goto out;
  152. }
  153. if (di) {
  154. /*
  155. * We're doing a replace, and it must be atomic, that is, at
  156. * any point in time we have either the old or the new xattr
  157. * value in the tree. We don't want readers (getxattr and
  158. * listxattrs) to miss a value, this is specially important
  159. * for ACLs.
  160. */
  161. const int slot = path->slots[0];
  162. struct extent_buffer *leaf = path->nodes[0];
  163. const u16 old_data_len = btrfs_dir_data_len(leaf, di);
  164. const u32 item_size = btrfs_item_size_nr(leaf, slot);
  165. const u32 data_size = sizeof(*di) + name_len + size;
  166. struct btrfs_item *item;
  167. unsigned long data_ptr;
  168. char *ptr;
  169. if (size > old_data_len) {
  170. if (btrfs_leaf_free_space(root, leaf) <
  171. (size - old_data_len)) {
  172. ret = -ENOSPC;
  173. goto out;
  174. }
  175. }
  176. if (old_data_len + name_len + sizeof(*di) == item_size) {
  177. /* No other xattrs packed in the same leaf item. */
  178. if (size > old_data_len)
  179. btrfs_extend_item(root, path,
  180. size - old_data_len);
  181. else if (size < old_data_len)
  182. btrfs_truncate_item(root, path, data_size, 1);
  183. } else {
  184. /* There are other xattrs packed in the same item. */
  185. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  186. if (ret)
  187. goto out;
  188. btrfs_extend_item(root, path, data_size);
  189. }
  190. item = btrfs_item_nr(slot);
  191. ptr = btrfs_item_ptr(leaf, slot, char);
  192. ptr += btrfs_item_size(leaf, item) - data_size;
  193. di = (struct btrfs_dir_item *)ptr;
  194. btrfs_set_dir_data_len(leaf, di, size);
  195. data_ptr = ((unsigned long)(di + 1)) + name_len;
  196. write_extent_buffer(leaf, value, data_ptr, size);
  197. btrfs_mark_buffer_dirty(leaf);
  198. } else {
  199. /*
  200. * Insert, and we had space for the xattr, so path->slots[0] is
  201. * where our xattr dir_item is and btrfs_insert_xattr_item()
  202. * filled it.
  203. */
  204. }
  205. out:
  206. btrfs_free_path(path);
  207. return ret;
  208. }
  209. /*
  210. * @value: "" makes the attribute to empty, NULL removes it
  211. */
  212. int __btrfs_setxattr(struct btrfs_trans_handle *trans,
  213. struct inode *inode, const char *name,
  214. const void *value, size_t size, int flags)
  215. {
  216. struct btrfs_root *root = BTRFS_I(inode)->root;
  217. int ret;
  218. if (btrfs_root_readonly(root))
  219. return -EROFS;
  220. if (trans)
  221. return do_setxattr(trans, inode, name, value, size, flags);
  222. trans = btrfs_start_transaction(root, 2);
  223. if (IS_ERR(trans))
  224. return PTR_ERR(trans);
  225. ret = do_setxattr(trans, inode, name, value, size, flags);
  226. if (ret)
  227. goto out;
  228. inode_inc_iversion(inode);
  229. inode->i_ctime = current_time(inode);
  230. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  231. ret = btrfs_update_inode(trans, root, inode);
  232. BUG_ON(ret);
  233. out:
  234. btrfs_end_transaction(trans, root);
  235. return ret;
  236. }
  237. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  238. {
  239. struct btrfs_key key;
  240. struct inode *inode = d_inode(dentry);
  241. struct btrfs_root *root = BTRFS_I(inode)->root;
  242. struct btrfs_path *path;
  243. int ret = 0;
  244. size_t total_size = 0, size_left = size;
  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 = btrfs_ino(inode);
  251. key.type = BTRFS_XATTR_ITEM_KEY;
  252. key.offset = 0;
  253. path = btrfs_alloc_path();
  254. if (!path)
  255. return -ENOMEM;
  256. path->reada = READA_FORWARD;
  257. /* search for our xattrs */
  258. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  259. if (ret < 0)
  260. goto err;
  261. while (1) {
  262. struct extent_buffer *leaf;
  263. int slot;
  264. struct btrfs_dir_item *di;
  265. struct btrfs_key found_key;
  266. u32 item_size;
  267. u32 cur;
  268. leaf = path->nodes[0];
  269. slot = path->slots[0];
  270. /* this is where we start walking through the path */
  271. if (slot >= btrfs_header_nritems(leaf)) {
  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. ret = btrfs_next_leaf(root, path);
  277. if (ret < 0)
  278. goto err;
  279. else if (ret > 0)
  280. break;
  281. continue;
  282. }
  283. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  284. /* check to make sure this item is what we want */
  285. if (found_key.objectid != key.objectid)
  286. break;
  287. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  288. break;
  289. if (found_key.type < BTRFS_XATTR_ITEM_KEY)
  290. goto next_item;
  291. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  292. item_size = btrfs_item_size_nr(leaf, slot);
  293. cur = 0;
  294. while (cur < item_size) {
  295. u16 name_len = btrfs_dir_name_len(leaf, di);
  296. u16 data_len = btrfs_dir_data_len(leaf, di);
  297. u32 this_len = sizeof(*di) + name_len + data_len;
  298. unsigned long name_ptr = (unsigned long)(di + 1);
  299. if (verify_dir_item(root, leaf, di)) {
  300. ret = -EIO;
  301. goto err;
  302. }
  303. total_size += name_len + 1;
  304. /*
  305. * We are just looking for how big our buffer needs to
  306. * be.
  307. */
  308. if (!size)
  309. goto next;
  310. if (!buffer || (name_len + 1) > size_left) {
  311. ret = -ERANGE;
  312. goto err;
  313. }
  314. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  315. buffer[name_len] = '\0';
  316. size_left -= name_len + 1;
  317. buffer += name_len + 1;
  318. next:
  319. cur += this_len;
  320. di = (struct btrfs_dir_item *)((char *)di + this_len);
  321. }
  322. next_item:
  323. path->slots[0]++;
  324. }
  325. ret = total_size;
  326. err:
  327. btrfs_free_path(path);
  328. return ret;
  329. }
  330. static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
  331. struct dentry *unused, struct inode *inode,
  332. const char *name, void *buffer, size_t size)
  333. {
  334. name = xattr_full_name(handler, name);
  335. return __btrfs_getxattr(inode, name, buffer, size);
  336. }
  337. static int btrfs_xattr_handler_set(const struct xattr_handler *handler,
  338. struct dentry *unused, struct inode *inode,
  339. const char *name, const void *buffer,
  340. size_t size, int flags)
  341. {
  342. name = xattr_full_name(handler, name);
  343. return __btrfs_setxattr(NULL, inode, name, buffer, size, flags);
  344. }
  345. static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
  346. struct dentry *unused, struct inode *inode,
  347. const char *name, const void *value,
  348. size_t size, int flags)
  349. {
  350. name = xattr_full_name(handler, name);
  351. return btrfs_set_prop(inode, name, value, size, flags);
  352. }
  353. static const struct xattr_handler btrfs_security_xattr_handler = {
  354. .prefix = XATTR_SECURITY_PREFIX,
  355. .get = btrfs_xattr_handler_get,
  356. .set = btrfs_xattr_handler_set,
  357. };
  358. static const struct xattr_handler btrfs_trusted_xattr_handler = {
  359. .prefix = XATTR_TRUSTED_PREFIX,
  360. .get = btrfs_xattr_handler_get,
  361. .set = btrfs_xattr_handler_set,
  362. };
  363. static const struct xattr_handler btrfs_user_xattr_handler = {
  364. .prefix = XATTR_USER_PREFIX,
  365. .get = btrfs_xattr_handler_get,
  366. .set = btrfs_xattr_handler_set,
  367. };
  368. static const struct xattr_handler btrfs_btrfs_xattr_handler = {
  369. .prefix = XATTR_BTRFS_PREFIX,
  370. .get = btrfs_xattr_handler_get,
  371. .set = btrfs_xattr_handler_set_prop,
  372. };
  373. const struct xattr_handler *btrfs_xattr_handlers[] = {
  374. &btrfs_security_xattr_handler,
  375. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  376. &posix_acl_access_xattr_handler,
  377. &posix_acl_default_xattr_handler,
  378. #endif
  379. &btrfs_trusted_xattr_handler,
  380. &btrfs_user_xattr_handler,
  381. &btrfs_btrfs_xattr_handler,
  382. NULL,
  383. };
  384. static int btrfs_initxattrs(struct inode *inode,
  385. const struct xattr *xattr_array, void *fs_info)
  386. {
  387. const struct xattr *xattr;
  388. struct btrfs_trans_handle *trans = fs_info;
  389. char *name;
  390. int err = 0;
  391. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  392. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  393. strlen(xattr->name) + 1, GFP_KERNEL);
  394. if (!name) {
  395. err = -ENOMEM;
  396. break;
  397. }
  398. strcpy(name, XATTR_SECURITY_PREFIX);
  399. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  400. err = __btrfs_setxattr(trans, inode, name,
  401. xattr->value, xattr->value_len, 0);
  402. kfree(name);
  403. if (err < 0)
  404. break;
  405. }
  406. return err;
  407. }
  408. int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
  409. struct inode *inode, struct inode *dir,
  410. const struct qstr *qstr)
  411. {
  412. return security_inode_init_security(inode, dir, qstr,
  413. &btrfs_initxattrs, trans);
  414. }