xattr.c 12 KB

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