xattr.c 12 KB

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