xattr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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(mutex_is_locked(&inode->i_mutex));
  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 (trans)
  219. return do_setxattr(trans, inode, name, value, size, flags);
  220. trans = btrfs_start_transaction(root, 2);
  221. if (IS_ERR(trans))
  222. return PTR_ERR(trans);
  223. ret = do_setxattr(trans, inode, name, value, size, flags);
  224. if (ret)
  225. goto out;
  226. inode_inc_iversion(inode);
  227. inode->i_ctime = CURRENT_TIME;
  228. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  229. ret = btrfs_update_inode(trans, root, inode);
  230. BUG_ON(ret);
  231. out:
  232. btrfs_end_transaction(trans, root);
  233. return ret;
  234. }
  235. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  236. {
  237. struct btrfs_key key, found_key;
  238. struct inode *inode = d_inode(dentry);
  239. struct btrfs_root *root = BTRFS_I(inode)->root;
  240. struct btrfs_path *path;
  241. struct extent_buffer *leaf;
  242. struct btrfs_dir_item *di;
  243. int ret = 0, slot;
  244. size_t total_size = 0, size_left = size;
  245. unsigned long name_ptr;
  246. size_t name_len;
  247. /*
  248. * ok we want all objects associated with this id.
  249. * NOTE: we set key.offset = 0; because we want to start with the
  250. * first xattr that we find and walk forward
  251. */
  252. key.objectid = btrfs_ino(inode);
  253. key.type = BTRFS_XATTR_ITEM_KEY;
  254. key.offset = 0;
  255. path = btrfs_alloc_path();
  256. if (!path)
  257. return -ENOMEM;
  258. path->reada = 2;
  259. /* search for our xattrs */
  260. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  261. if (ret < 0)
  262. goto err;
  263. while (1) {
  264. leaf = path->nodes[0];
  265. slot = path->slots[0];
  266. /* this is where we start walking through the path */
  267. if (slot >= btrfs_header_nritems(leaf)) {
  268. /*
  269. * if we've reached the last slot in this leaf we need
  270. * to go to the next leaf and reset everything
  271. */
  272. ret = btrfs_next_leaf(root, path);
  273. if (ret < 0)
  274. goto err;
  275. else if (ret > 0)
  276. break;
  277. continue;
  278. }
  279. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  280. /* check to make sure this item is what we want */
  281. if (found_key.objectid != key.objectid)
  282. break;
  283. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  284. break;
  285. if (found_key.type < BTRFS_XATTR_ITEM_KEY)
  286. goto next;
  287. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  288. if (verify_dir_item(root, leaf, di))
  289. goto next;
  290. name_len = btrfs_dir_name_len(leaf, di);
  291. total_size += name_len + 1;
  292. /* we are just looking for how big our buffer needs to be */
  293. if (!size)
  294. goto next;
  295. if (!buffer || (name_len + 1) > size_left) {
  296. ret = -ERANGE;
  297. goto err;
  298. }
  299. name_ptr = (unsigned long)(di + 1);
  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. path->slots[0]++;
  306. }
  307. ret = total_size;
  308. err:
  309. btrfs_free_path(path);
  310. return ret;
  311. }
  312. /*
  313. * List of handlers for synthetic system.* attributes. All real ondisk
  314. * attributes are handled directly.
  315. */
  316. const struct xattr_handler *btrfs_xattr_handlers[] = {
  317. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  318. &posix_acl_access_xattr_handler,
  319. &posix_acl_default_xattr_handler,
  320. #endif
  321. NULL,
  322. };
  323. /*
  324. * Check if the attribute is in a supported namespace.
  325. *
  326. * This is applied after the check for the synthetic attributes in the system
  327. * namespace.
  328. */
  329. static int btrfs_is_valid_xattr(const char *name)
  330. {
  331. int len = strlen(name);
  332. int prefixlen = 0;
  333. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  334. XATTR_SECURITY_PREFIX_LEN))
  335. prefixlen = XATTR_SECURITY_PREFIX_LEN;
  336. else if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  337. prefixlen = XATTR_SYSTEM_PREFIX_LEN;
  338. else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
  339. prefixlen = XATTR_TRUSTED_PREFIX_LEN;
  340. else if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
  341. prefixlen = XATTR_USER_PREFIX_LEN;
  342. else if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN))
  343. prefixlen = XATTR_BTRFS_PREFIX_LEN;
  344. else
  345. return -EOPNOTSUPP;
  346. /*
  347. * The name cannot consist of just prefix
  348. */
  349. if (len <= prefixlen)
  350. return -EINVAL;
  351. return 0;
  352. }
  353. ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
  354. void *buffer, size_t size)
  355. {
  356. int ret;
  357. /*
  358. * If this is a request for a synthetic attribute in the system.*
  359. * namespace use the generic infrastructure to resolve a handler
  360. * for it via sb->s_xattr.
  361. */
  362. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  363. return generic_getxattr(dentry, name, buffer, size);
  364. ret = btrfs_is_valid_xattr(name);
  365. if (ret)
  366. return ret;
  367. return __btrfs_getxattr(d_inode(dentry), name, buffer, size);
  368. }
  369. int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  370. size_t size, int flags)
  371. {
  372. struct btrfs_root *root = BTRFS_I(d_inode(dentry))->root;
  373. int ret;
  374. /*
  375. * The permission on security.* and system.* is not checked
  376. * in permission().
  377. */
  378. if (btrfs_root_readonly(root))
  379. return -EROFS;
  380. /*
  381. * If this is a request for a synthetic attribute in the system.*
  382. * namespace use the generic infrastructure to resolve a handler
  383. * for it via sb->s_xattr.
  384. */
  385. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  386. return generic_setxattr(dentry, name, value, size, flags);
  387. ret = btrfs_is_valid_xattr(name);
  388. if (ret)
  389. return ret;
  390. if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN))
  391. return btrfs_set_prop(d_inode(dentry), name,
  392. value, size, flags);
  393. if (size == 0)
  394. value = ""; /* empty EA, do not remove */
  395. return __btrfs_setxattr(NULL, d_inode(dentry), name, value, size,
  396. flags);
  397. }
  398. int btrfs_removexattr(struct dentry *dentry, const char *name)
  399. {
  400. struct btrfs_root *root = BTRFS_I(d_inode(dentry))->root;
  401. int ret;
  402. /*
  403. * The permission on security.* and system.* is not checked
  404. * in permission().
  405. */
  406. if (btrfs_root_readonly(root))
  407. return -EROFS;
  408. /*
  409. * If this is a request for a synthetic attribute in the system.*
  410. * namespace use the generic infrastructure to resolve a handler
  411. * for it via sb->s_xattr.
  412. */
  413. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  414. return generic_removexattr(dentry, name);
  415. ret = btrfs_is_valid_xattr(name);
  416. if (ret)
  417. return ret;
  418. if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN))
  419. return btrfs_set_prop(d_inode(dentry), name,
  420. NULL, 0, XATTR_REPLACE);
  421. return __btrfs_setxattr(NULL, d_inode(dentry), name, NULL, 0,
  422. XATTR_REPLACE);
  423. }
  424. static int btrfs_initxattrs(struct inode *inode,
  425. const struct xattr *xattr_array, void *fs_info)
  426. {
  427. const struct xattr *xattr;
  428. struct btrfs_trans_handle *trans = fs_info;
  429. char *name;
  430. int err = 0;
  431. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  432. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  433. strlen(xattr->name) + 1, GFP_NOFS);
  434. if (!name) {
  435. err = -ENOMEM;
  436. break;
  437. }
  438. strcpy(name, XATTR_SECURITY_PREFIX);
  439. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  440. err = __btrfs_setxattr(trans, inode, name,
  441. xattr->value, xattr->value_len, 0);
  442. kfree(name);
  443. if (err < 0)
  444. break;
  445. }
  446. return err;
  447. }
  448. int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
  449. struct inode *inode, struct inode *dir,
  450. const struct qstr *qstr)
  451. {
  452. return security_inode_init_security(inode, dir, qstr,
  453. &btrfs_initxattrs, trans);
  454. }