xattr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 (di)
  102. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  103. goto out;
  104. }
  105. /*
  106. * For a replace we can't just do the insert blindly.
  107. * Do a lookup first (read-only btrfs_search_slot), and return if xattr
  108. * doesn't exist. If it exists, fall down below to the insert/replace
  109. * path - we can't race with a concurrent xattr delete, because the VFS
  110. * locks the inode's i_mutex before calling setxattr or removexattr.
  111. */
  112. if (flags & XATTR_REPLACE) {
  113. ASSERT(mutex_is_locked(&inode->i_mutex));
  114. di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode),
  115. name, name_len, 0);
  116. if (!di) {
  117. ret = -ENODATA;
  118. goto out;
  119. }
  120. btrfs_release_path(path);
  121. di = NULL;
  122. }
  123. ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
  124. name, name_len, value, size);
  125. if (ret == -EOVERFLOW) {
  126. /*
  127. * We have an existing item in a leaf, split_leaf couldn't
  128. * expand it. That item might have or not a dir_item that
  129. * matches our target xattr, so lets check.
  130. */
  131. ret = 0;
  132. btrfs_assert_tree_locked(path->nodes[0]);
  133. di = btrfs_match_dir_item_name(root, path, name, name_len);
  134. if (!di && !(flags & XATTR_REPLACE)) {
  135. ret = -ENOSPC;
  136. goto out;
  137. }
  138. } else if (ret == -EEXIST) {
  139. ret = 0;
  140. di = btrfs_match_dir_item_name(root, path, name, name_len);
  141. ASSERT(di); /* logic error */
  142. } else if (ret) {
  143. goto out;
  144. }
  145. if (di && (flags & XATTR_CREATE)) {
  146. ret = -EEXIST;
  147. goto out;
  148. }
  149. if (di) {
  150. /*
  151. * We're doing a replace, and it must be atomic, that is, at
  152. * any point in time we have either the old or the new xattr
  153. * value in the tree. We don't want readers (getxattr and
  154. * listxattrs) to miss a value, this is specially important
  155. * for ACLs.
  156. */
  157. const int slot = path->slots[0];
  158. struct extent_buffer *leaf = path->nodes[0];
  159. const u16 old_data_len = btrfs_dir_data_len(leaf, di);
  160. const u32 item_size = btrfs_item_size_nr(leaf, slot);
  161. const u32 data_size = sizeof(*di) + name_len + size;
  162. struct btrfs_item *item;
  163. unsigned long data_ptr;
  164. char *ptr;
  165. if (size > old_data_len) {
  166. if (btrfs_leaf_free_space(root, leaf) <
  167. (size - old_data_len)) {
  168. ret = -ENOSPC;
  169. goto out;
  170. }
  171. }
  172. if (old_data_len + name_len + sizeof(*di) == item_size) {
  173. /* No other xattrs packed in the same leaf item. */
  174. if (size > old_data_len)
  175. btrfs_extend_item(root, path,
  176. size - old_data_len);
  177. else if (size < old_data_len)
  178. btrfs_truncate_item(root, path, data_size, 1);
  179. } else {
  180. /* There are other xattrs packed in the same item. */
  181. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  182. if (ret)
  183. goto out;
  184. btrfs_extend_item(root, path, data_size);
  185. }
  186. item = btrfs_item_nr(slot);
  187. ptr = btrfs_item_ptr(leaf, slot, char);
  188. ptr += btrfs_item_size(leaf, item) - data_size;
  189. di = (struct btrfs_dir_item *)ptr;
  190. btrfs_set_dir_data_len(leaf, di, size);
  191. data_ptr = ((unsigned long)(di + 1)) + name_len;
  192. write_extent_buffer(leaf, value, data_ptr, size);
  193. btrfs_mark_buffer_dirty(leaf);
  194. } else {
  195. /*
  196. * Insert, and we had space for the xattr, so path->slots[0] is
  197. * where our xattr dir_item is and btrfs_insert_xattr_item()
  198. * filled it.
  199. */
  200. }
  201. out:
  202. btrfs_free_path(path);
  203. return ret;
  204. }
  205. /*
  206. * @value: "" makes the attribute to empty, NULL removes it
  207. */
  208. int __btrfs_setxattr(struct btrfs_trans_handle *trans,
  209. struct inode *inode, const char *name,
  210. const void *value, size_t size, int flags)
  211. {
  212. struct btrfs_root *root = BTRFS_I(inode)->root;
  213. int ret;
  214. if (trans)
  215. return do_setxattr(trans, inode, name, value, size, flags);
  216. trans = btrfs_start_transaction(root, 2);
  217. if (IS_ERR(trans))
  218. return PTR_ERR(trans);
  219. ret = do_setxattr(trans, inode, name, value, size, flags);
  220. if (ret)
  221. goto out;
  222. inode_inc_iversion(inode);
  223. inode->i_ctime = CURRENT_TIME;
  224. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  225. ret = btrfs_update_inode(trans, root, inode);
  226. BUG_ON(ret);
  227. out:
  228. btrfs_end_transaction(trans, root);
  229. return ret;
  230. }
  231. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  232. {
  233. struct btrfs_key key, found_key;
  234. struct inode *inode = dentry->d_inode;
  235. struct btrfs_root *root = BTRFS_I(inode)->root;
  236. struct btrfs_path *path;
  237. struct extent_buffer *leaf;
  238. struct btrfs_dir_item *di;
  239. int ret = 0, slot;
  240. size_t total_size = 0, size_left = size;
  241. unsigned long name_ptr;
  242. size_t name_len;
  243. /*
  244. * ok we want all objects associated with this id.
  245. * NOTE: we set key.offset = 0; because we want to start with the
  246. * first xattr that we find and walk forward
  247. */
  248. key.objectid = btrfs_ino(inode);
  249. key.type = BTRFS_XATTR_ITEM_KEY;
  250. key.offset = 0;
  251. path = btrfs_alloc_path();
  252. if (!path)
  253. return -ENOMEM;
  254. path->reada = 2;
  255. /* search for our xattrs */
  256. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  257. if (ret < 0)
  258. goto err;
  259. while (1) {
  260. leaf = path->nodes[0];
  261. slot = path->slots[0];
  262. /* this is where we start walking through the path */
  263. if (slot >= btrfs_header_nritems(leaf)) {
  264. /*
  265. * if we've reached the last slot in this leaf we need
  266. * to go to the next leaf and reset everything
  267. */
  268. ret = btrfs_next_leaf(root, path);
  269. if (ret < 0)
  270. goto err;
  271. else if (ret > 0)
  272. break;
  273. continue;
  274. }
  275. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  276. /* check to make sure this item is what we want */
  277. if (found_key.objectid != key.objectid)
  278. break;
  279. if (found_key.type != BTRFS_XATTR_ITEM_KEY)
  280. break;
  281. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  282. if (verify_dir_item(root, leaf, di))
  283. goto next;
  284. name_len = btrfs_dir_name_len(leaf, di);
  285. total_size += name_len + 1;
  286. /* we are just looking for how big our buffer needs to be */
  287. if (!size)
  288. goto next;
  289. if (!buffer || (name_len + 1) > size_left) {
  290. ret = -ERANGE;
  291. goto err;
  292. }
  293. name_ptr = (unsigned long)(di + 1);
  294. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  295. buffer[name_len] = '\0';
  296. size_left -= name_len + 1;
  297. buffer += name_len + 1;
  298. next:
  299. path->slots[0]++;
  300. }
  301. ret = total_size;
  302. err:
  303. btrfs_free_path(path);
  304. return ret;
  305. }
  306. /*
  307. * List of handlers for synthetic system.* attributes. All real ondisk
  308. * attributes are handled directly.
  309. */
  310. const struct xattr_handler *btrfs_xattr_handlers[] = {
  311. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  312. &posix_acl_access_xattr_handler,
  313. &posix_acl_default_xattr_handler,
  314. #endif
  315. NULL,
  316. };
  317. /*
  318. * Check if the attribute is in a supported namespace.
  319. *
  320. * This applied after the check for the synthetic attributes in the system
  321. * namespace.
  322. */
  323. static bool btrfs_is_valid_xattr(const char *name)
  324. {
  325. return !strncmp(name, XATTR_SECURITY_PREFIX,
  326. XATTR_SECURITY_PREFIX_LEN) ||
  327. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
  328. !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  329. !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) ||
  330. !strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN);
  331. }
  332. ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
  333. void *buffer, size_t size)
  334. {
  335. /*
  336. * If this is a request for a synthetic attribute in the system.*
  337. * namespace use the generic infrastructure to resolve a handler
  338. * for it via sb->s_xattr.
  339. */
  340. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  341. return generic_getxattr(dentry, name, buffer, size);
  342. if (!btrfs_is_valid_xattr(name))
  343. return -EOPNOTSUPP;
  344. return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
  345. }
  346. int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  347. size_t size, int flags)
  348. {
  349. struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
  350. /*
  351. * The permission on security.* and system.* is not checked
  352. * in permission().
  353. */
  354. if (btrfs_root_readonly(root))
  355. return -EROFS;
  356. /*
  357. * If this is a request for a synthetic attribute in the system.*
  358. * namespace use the generic infrastructure to resolve a handler
  359. * for it via sb->s_xattr.
  360. */
  361. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  362. return generic_setxattr(dentry, name, value, size, flags);
  363. if (!btrfs_is_valid_xattr(name))
  364. return -EOPNOTSUPP;
  365. if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN))
  366. return btrfs_set_prop(dentry->d_inode, name,
  367. value, size, flags);
  368. if (size == 0)
  369. value = ""; /* empty EA, do not remove */
  370. return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
  371. flags);
  372. }
  373. int btrfs_removexattr(struct dentry *dentry, const char *name)
  374. {
  375. struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
  376. /*
  377. * The permission on security.* and system.* is not checked
  378. * in permission().
  379. */
  380. if (btrfs_root_readonly(root))
  381. return -EROFS;
  382. /*
  383. * If this is a request for a synthetic attribute in the system.*
  384. * namespace use the generic infrastructure to resolve a handler
  385. * for it via sb->s_xattr.
  386. */
  387. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  388. return generic_removexattr(dentry, name);
  389. if (!btrfs_is_valid_xattr(name))
  390. return -EOPNOTSUPP;
  391. if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN))
  392. return btrfs_set_prop(dentry->d_inode, name,
  393. NULL, 0, XATTR_REPLACE);
  394. return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
  395. XATTR_REPLACE);
  396. }
  397. static int btrfs_initxattrs(struct inode *inode,
  398. const struct xattr *xattr_array, void *fs_info)
  399. {
  400. const struct xattr *xattr;
  401. struct btrfs_trans_handle *trans = fs_info;
  402. char *name;
  403. int err = 0;
  404. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  405. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  406. strlen(xattr->name) + 1, GFP_NOFS);
  407. if (!name) {
  408. err = -ENOMEM;
  409. break;
  410. }
  411. strcpy(name, XATTR_SECURITY_PREFIX);
  412. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  413. err = __btrfs_setxattr(trans, inode, name,
  414. xattr->value, xattr->value_len, 0);
  415. kfree(name);
  416. if (err < 0)
  417. break;
  418. }
  419. return err;
  420. }
  421. int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
  422. struct inode *inode, struct inode *dir,
  423. const struct qstr *qstr)
  424. {
  425. return security_inode_init_security(inode, dir, qstr,
  426. &btrfs_initxattrs, trans);
  427. }