xattr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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_fs_info *fs_info = root->fs_info;
  88. struct btrfs_path *path;
  89. size_t name_len = strlen(name);
  90. int ret = 0;
  91. if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info))
  92. return -ENOSPC;
  93. path = btrfs_alloc_path();
  94. if (!path)
  95. return -ENOMEM;
  96. path->skip_release_on_error = 1;
  97. if (!value) {
  98. di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode),
  99. name, name_len, -1);
  100. if (!di && (flags & XATTR_REPLACE))
  101. ret = -ENODATA;
  102. else if (IS_ERR(di))
  103. ret = PTR_ERR(di);
  104. else if (di)
  105. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  106. goto out;
  107. }
  108. /*
  109. * For a replace we can't just do the insert blindly.
  110. * Do a lookup first (read-only btrfs_search_slot), and return if xattr
  111. * doesn't exist. If it exists, fall down below to the insert/replace
  112. * path - we can't race with a concurrent xattr delete, because the VFS
  113. * locks the inode's i_mutex before calling setxattr or removexattr.
  114. */
  115. if (flags & XATTR_REPLACE) {
  116. ASSERT(inode_is_locked(inode));
  117. di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode),
  118. name, name_len, 0);
  119. if (!di)
  120. ret = -ENODATA;
  121. else if (IS_ERR(di))
  122. ret = PTR_ERR(di);
  123. if (ret)
  124. goto out;
  125. btrfs_release_path(path);
  126. di = NULL;
  127. }
  128. ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
  129. name, name_len, value, size);
  130. if (ret == -EOVERFLOW) {
  131. /*
  132. * We have an existing item in a leaf, split_leaf couldn't
  133. * expand it. That item might have or not a dir_item that
  134. * matches our target xattr, so lets check.
  135. */
  136. ret = 0;
  137. btrfs_assert_tree_locked(path->nodes[0]);
  138. di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
  139. if (!di && !(flags & XATTR_REPLACE)) {
  140. ret = -ENOSPC;
  141. goto out;
  142. }
  143. } else if (ret == -EEXIST) {
  144. ret = 0;
  145. di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
  146. ASSERT(di); /* logic error */
  147. } else if (ret) {
  148. goto out;
  149. }
  150. if (di && (flags & XATTR_CREATE)) {
  151. ret = -EEXIST;
  152. goto out;
  153. }
  154. if (di) {
  155. /*
  156. * We're doing a replace, and it must be atomic, that is, at
  157. * any point in time we have either the old or the new xattr
  158. * value in the tree. We don't want readers (getxattr and
  159. * listxattrs) to miss a value, this is specially important
  160. * for ACLs.
  161. */
  162. const int slot = path->slots[0];
  163. struct extent_buffer *leaf = path->nodes[0];
  164. const u16 old_data_len = btrfs_dir_data_len(leaf, di);
  165. const u32 item_size = btrfs_item_size_nr(leaf, slot);
  166. const u32 data_size = sizeof(*di) + name_len + size;
  167. struct btrfs_item *item;
  168. unsigned long data_ptr;
  169. char *ptr;
  170. if (size > old_data_len) {
  171. if (btrfs_leaf_free_space(fs_info, leaf) <
  172. (size - old_data_len)) {
  173. ret = -ENOSPC;
  174. goto out;
  175. }
  176. }
  177. if (old_data_len + name_len + sizeof(*di) == item_size) {
  178. /* No other xattrs packed in the same leaf item. */
  179. if (size > old_data_len)
  180. btrfs_extend_item(fs_info, path,
  181. size - old_data_len);
  182. else if (size < old_data_len)
  183. btrfs_truncate_item(fs_info, path,
  184. data_size, 1);
  185. } else {
  186. /* There are other xattrs packed in the same item. */
  187. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  188. if (ret)
  189. goto out;
  190. btrfs_extend_item(fs_info, path, data_size);
  191. }
  192. item = btrfs_item_nr(slot);
  193. ptr = btrfs_item_ptr(leaf, slot, char);
  194. ptr += btrfs_item_size(leaf, item) - data_size;
  195. di = (struct btrfs_dir_item *)ptr;
  196. btrfs_set_dir_data_len(leaf, di, size);
  197. data_ptr = ((unsigned long)(di + 1)) + name_len;
  198. write_extent_buffer(leaf, value, data_ptr, size);
  199. btrfs_mark_buffer_dirty(leaf);
  200. } else {
  201. /*
  202. * Insert, and we had space for the xattr, so path->slots[0] is
  203. * where our xattr dir_item is and btrfs_insert_xattr_item()
  204. * filled it.
  205. */
  206. }
  207. out:
  208. btrfs_free_path(path);
  209. return ret;
  210. }
  211. /*
  212. * @value: "" makes the attribute to empty, NULL removes it
  213. */
  214. int __btrfs_setxattr(struct btrfs_trans_handle *trans,
  215. struct inode *inode, const char *name,
  216. const void *value, size_t size, int flags)
  217. {
  218. struct btrfs_root *root = BTRFS_I(inode)->root;
  219. int ret;
  220. if (btrfs_root_readonly(root))
  221. return -EROFS;
  222. if (trans)
  223. return do_setxattr(trans, inode, name, value, size, flags);
  224. trans = btrfs_start_transaction(root, 2);
  225. if (IS_ERR(trans))
  226. return PTR_ERR(trans);
  227. ret = do_setxattr(trans, inode, name, value, size, flags);
  228. if (ret)
  229. goto out;
  230. inode_inc_iversion(inode);
  231. inode->i_ctime = current_time(inode);
  232. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  233. ret = btrfs_update_inode(trans, root, inode);
  234. BUG_ON(ret);
  235. out:
  236. btrfs_end_transaction(trans);
  237. return ret;
  238. }
  239. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  240. {
  241. struct btrfs_key key;
  242. struct inode *inode = d_inode(dentry);
  243. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  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(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. if (verify_dir_item(fs_info, leaf, di)) {
  303. ret = -EIO;
  304. goto err;
  305. }
  306. total_size += name_len + 1;
  307. /*
  308. * We are just looking for how big our buffer needs to
  309. * be.
  310. */
  311. if (!size)
  312. goto next;
  313. if (!buffer || (name_len + 1) > size_left) {
  314. ret = -ERANGE;
  315. goto err;
  316. }
  317. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  318. buffer[name_len] = '\0';
  319. size_left -= name_len + 1;
  320. buffer += name_len + 1;
  321. next:
  322. cur += this_len;
  323. di = (struct btrfs_dir_item *)((char *)di + this_len);
  324. }
  325. next_item:
  326. path->slots[0]++;
  327. }
  328. ret = total_size;
  329. err:
  330. btrfs_free_path(path);
  331. return ret;
  332. }
  333. static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
  334. struct dentry *unused, struct inode *inode,
  335. const char *name, void *buffer, size_t size)
  336. {
  337. name = xattr_full_name(handler, name);
  338. return __btrfs_getxattr(inode, name, buffer, size);
  339. }
  340. static int btrfs_xattr_handler_set(const struct xattr_handler *handler,
  341. struct dentry *unused, struct inode *inode,
  342. const char *name, const void *buffer,
  343. size_t size, int flags)
  344. {
  345. name = xattr_full_name(handler, name);
  346. return __btrfs_setxattr(NULL, inode, name, buffer, size, flags);
  347. }
  348. static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
  349. struct dentry *unused, struct inode *inode,
  350. const char *name, const void *value,
  351. size_t size, int flags)
  352. {
  353. name = xattr_full_name(handler, name);
  354. return btrfs_set_prop(inode, name, value, size, flags);
  355. }
  356. static const struct xattr_handler btrfs_security_xattr_handler = {
  357. .prefix = XATTR_SECURITY_PREFIX,
  358. .get = btrfs_xattr_handler_get,
  359. .set = btrfs_xattr_handler_set,
  360. };
  361. static const struct xattr_handler btrfs_trusted_xattr_handler = {
  362. .prefix = XATTR_TRUSTED_PREFIX,
  363. .get = btrfs_xattr_handler_get,
  364. .set = btrfs_xattr_handler_set,
  365. };
  366. static const struct xattr_handler btrfs_user_xattr_handler = {
  367. .prefix = XATTR_USER_PREFIX,
  368. .get = btrfs_xattr_handler_get,
  369. .set = btrfs_xattr_handler_set,
  370. };
  371. static const struct xattr_handler btrfs_btrfs_xattr_handler = {
  372. .prefix = XATTR_BTRFS_PREFIX,
  373. .get = btrfs_xattr_handler_get,
  374. .set = btrfs_xattr_handler_set_prop,
  375. };
  376. const struct xattr_handler *btrfs_xattr_handlers[] = {
  377. &btrfs_security_xattr_handler,
  378. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  379. &posix_acl_access_xattr_handler,
  380. &posix_acl_default_xattr_handler,
  381. #endif
  382. &btrfs_trusted_xattr_handler,
  383. &btrfs_user_xattr_handler,
  384. &btrfs_btrfs_xattr_handler,
  385. NULL,
  386. };
  387. static int btrfs_initxattrs(struct inode *inode,
  388. const struct xattr *xattr_array, void *fs_info)
  389. {
  390. const struct xattr *xattr;
  391. struct btrfs_trans_handle *trans = fs_info;
  392. char *name;
  393. int err = 0;
  394. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  395. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  396. strlen(xattr->name) + 1, GFP_KERNEL);
  397. if (!name) {
  398. err = -ENOMEM;
  399. break;
  400. }
  401. strcpy(name, XATTR_SECURITY_PREFIX);
  402. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  403. err = __btrfs_setxattr(trans, inode, name,
  404. xattr->value, xattr->value_len, 0);
  405. kfree(name);
  406. if (err < 0)
  407. break;
  408. }
  409. return err;
  410. }
  411. int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
  412. struct inode *inode, struct inode *dir,
  413. const struct qstr *qstr)
  414. {
  415. return security_inode_init_security(inode, dir, qstr,
  416. &btrfs_initxattrs, trans);
  417. }