dir-item.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include "messages.h"
  6. #include "ctree.h"
  7. #include "disk-io.h"
  8. #include "transaction.h"
  9. #include "accessors.h"
  10. #include "dir-item.h"
  11. #include "delayed-inode.h"
  12. /*
  13. * insert a name into a directory, doing overflow properly if there is a hash
  14. * collision. data_size indicates how big the item inserted should be. On
  15. * success a struct btrfs_dir_item pointer is returned, otherwise it is
  16. * an ERR_PTR.
  17. *
  18. * The name is not copied into the dir item, you have to do that yourself.
  19. */
  20. static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
  21. *trans,
  22. struct btrfs_root *root,
  23. struct btrfs_path *path,
  24. const struct btrfs_key *cpu_key,
  25. u32 data_size,
  26. const char *name,
  27. int name_len)
  28. {
  29. int ret;
  30. char *ptr;
  31. struct extent_buffer *leaf;
  32. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  33. if (ret == -EEXIST) {
  34. struct btrfs_dir_item *di;
  35. di = btrfs_match_dir_item_name(path, name, name_len);
  36. if (di)
  37. return ERR_PTR(-EEXIST);
  38. btrfs_extend_item(trans, path, data_size);
  39. } else if (ret < 0)
  40. return ERR_PTR(ret);
  41. WARN_ON(ret > 0);
  42. leaf = path->nodes[0];
  43. ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  44. ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0]));
  45. ptr += btrfs_item_size(leaf, path->slots[0]) - data_size;
  46. return (struct btrfs_dir_item *)ptr;
  47. }
  48. /*
  49. * xattrs work a lot like directories, this inserts an xattr item
  50. * into the tree
  51. */
  52. int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
  53. struct btrfs_root *root,
  54. struct btrfs_path *path, u64 objectid,
  55. const char *name, u16 name_len,
  56. const void *data, u16 data_len)
  57. {
  58. int ret = 0;
  59. struct btrfs_dir_item *dir_item;
  60. unsigned long name_ptr, data_ptr;
  61. struct btrfs_key key, location;
  62. struct btrfs_disk_key disk_key;
  63. struct extent_buffer *leaf;
  64. u32 data_size;
  65. if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
  66. return -ENOSPC;
  67. key.objectid = objectid;
  68. key.type = BTRFS_XATTR_ITEM_KEY;
  69. key.offset = btrfs_name_hash(name, name_len);
  70. data_size = sizeof(*dir_item) + name_len + data_len;
  71. dir_item = insert_with_overflow(trans, root, path, &key, data_size,
  72. name, name_len);
  73. if (IS_ERR(dir_item))
  74. return PTR_ERR(dir_item);
  75. memset(&location, 0, sizeof(location));
  76. leaf = path->nodes[0];
  77. btrfs_cpu_key_to_disk(&disk_key, &location);
  78. btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
  79. btrfs_set_dir_flags(leaf, dir_item, BTRFS_FT_XATTR);
  80. btrfs_set_dir_name_len(leaf, dir_item, name_len);
  81. btrfs_set_dir_transid(leaf, dir_item, trans->transid);
  82. btrfs_set_dir_data_len(leaf, dir_item, data_len);
  83. name_ptr = (unsigned long)(dir_item + 1);
  84. data_ptr = (unsigned long)((char *)name_ptr + name_len);
  85. write_extent_buffer(leaf, name, name_ptr, name_len);
  86. write_extent_buffer(leaf, data, data_ptr, data_len);
  87. return ret;
  88. }
  89. /*
  90. * insert a directory item in the tree, doing all the magic for
  91. * both indexes. 'dir' indicates which objectid to insert it into,
  92. * 'location' is the key to stuff into the directory item, 'type' is the
  93. * type of the inode we're pointing to, and 'index' is the sequence number
  94. * to use for the second index (if one is created).
  95. * Will return 0 or -ENOMEM
  96. */
  97. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
  98. const struct fscrypt_str *name, struct btrfs_inode *dir,
  99. const struct btrfs_key *location, u8 type, u64 index)
  100. {
  101. int ret = 0;
  102. int ret2 = 0;
  103. struct btrfs_root *root = dir->root;
  104. BTRFS_PATH_AUTO_FREE(path);
  105. struct btrfs_dir_item *dir_item;
  106. struct extent_buffer *leaf;
  107. unsigned long name_ptr;
  108. struct btrfs_key key;
  109. struct btrfs_disk_key disk_key;
  110. u32 data_size;
  111. key.objectid = btrfs_ino(dir);
  112. key.type = BTRFS_DIR_ITEM_KEY;
  113. key.offset = btrfs_name_hash(name->name, name->len);
  114. path = btrfs_alloc_path();
  115. if (!path)
  116. return -ENOMEM;
  117. btrfs_cpu_key_to_disk(&disk_key, location);
  118. data_size = sizeof(*dir_item) + name->len;
  119. dir_item = insert_with_overflow(trans, root, path, &key, data_size,
  120. name->name, name->len);
  121. if (IS_ERR(dir_item)) {
  122. ret = PTR_ERR(dir_item);
  123. if (ret == -EEXIST)
  124. goto second_insert;
  125. goto out_free;
  126. }
  127. if (IS_ENCRYPTED(&dir->vfs_inode))
  128. type |= BTRFS_FT_ENCRYPTED;
  129. leaf = path->nodes[0];
  130. btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
  131. btrfs_set_dir_flags(leaf, dir_item, type);
  132. btrfs_set_dir_data_len(leaf, dir_item, 0);
  133. btrfs_set_dir_name_len(leaf, dir_item, name->len);
  134. btrfs_set_dir_transid(leaf, dir_item, trans->transid);
  135. name_ptr = (unsigned long)(dir_item + 1);
  136. write_extent_buffer(leaf, name->name, name_ptr, name->len);
  137. second_insert:
  138. /* FIXME, use some real flag for selecting the extra index */
  139. if (root == root->fs_info->tree_root) {
  140. ret = 0;
  141. goto out_free;
  142. }
  143. btrfs_release_path(path);
  144. ret2 = btrfs_insert_delayed_dir_index(trans, name->name, name->len, dir,
  145. &disk_key, type, index);
  146. out_free:
  147. if (ret)
  148. return ret;
  149. if (ret2)
  150. return ret2;
  151. return 0;
  152. }
  153. static struct btrfs_dir_item *btrfs_lookup_match_dir(
  154. struct btrfs_trans_handle *trans,
  155. struct btrfs_root *root, struct btrfs_path *path,
  156. struct btrfs_key *key, const char *name,
  157. int name_len, int mod)
  158. {
  159. const int ins_len = (mod < 0 ? -1 : 0);
  160. const int cow = (mod != 0);
  161. int ret;
  162. ret = btrfs_search_slot(trans, root, key, path, ins_len, cow);
  163. if (ret < 0)
  164. return ERR_PTR(ret);
  165. if (ret > 0)
  166. return ERR_PTR(-ENOENT);
  167. return btrfs_match_dir_item_name(path, name, name_len);
  168. }
  169. /*
  170. * Lookup for a directory item by name.
  171. *
  172. * @trans: The transaction handle to use. Can be NULL if @mod is 0.
  173. * @root: The root of the target tree.
  174. * @path: Path to use for the search.
  175. * @dir: The inode number (objectid) of the directory.
  176. * @name: The name associated to the directory entry we are looking for.
  177. * @name_len: The length of the name.
  178. * @mod: Used to indicate if the tree search is meant for a read only
  179. * lookup, for a modification lookup or for a deletion lookup, so
  180. * its value should be 0, 1 or -1, respectively.
  181. *
  182. * Returns: NULL if the dir item does not exists, an error pointer if an error
  183. * happened, or a pointer to a dir item if a dir item exists for the given name.
  184. */
  185. struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
  186. struct btrfs_root *root,
  187. struct btrfs_path *path, u64 dir,
  188. const struct fscrypt_str *name,
  189. int mod)
  190. {
  191. struct btrfs_key key;
  192. struct btrfs_dir_item *di;
  193. key.objectid = dir;
  194. key.type = BTRFS_DIR_ITEM_KEY;
  195. key.offset = btrfs_name_hash(name->name, name->len);
  196. di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
  197. name->len, mod);
  198. if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
  199. return NULL;
  200. return di;
  201. }
  202. int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir_ino,
  203. const struct fscrypt_str *name)
  204. {
  205. int ret;
  206. struct btrfs_key key;
  207. struct btrfs_dir_item *di;
  208. int data_size;
  209. struct extent_buffer *leaf;
  210. int slot;
  211. BTRFS_PATH_AUTO_FREE(path);
  212. path = btrfs_alloc_path();
  213. if (!path)
  214. return -ENOMEM;
  215. key.objectid = dir_ino;
  216. key.type = BTRFS_DIR_ITEM_KEY;
  217. key.offset = btrfs_name_hash(name->name, name->len);
  218. di = btrfs_lookup_match_dir(NULL, root, path, &key, name->name,
  219. name->len, 0);
  220. if (IS_ERR(di)) {
  221. ret = PTR_ERR(di);
  222. /* Nothing found, we're safe */
  223. if (ret == -ENOENT)
  224. return 0;
  225. if (ret < 0)
  226. return ret;
  227. }
  228. /* we found an item, look for our name in the item */
  229. if (di) {
  230. /* our exact name was found */
  231. return -EEXIST;
  232. }
  233. /* See if there is room in the item to insert this name. */
  234. data_size = sizeof(*di) + name->len;
  235. leaf = path->nodes[0];
  236. slot = path->slots[0];
  237. if (data_size + btrfs_item_size(leaf, slot) +
  238. sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
  239. return -EOVERFLOW;
  240. }
  241. /* Plenty of insertion room. */
  242. return 0;
  243. }
  244. /*
  245. * Lookup for a directory index item by name and index number.
  246. *
  247. * @trans: The transaction handle to use. Can be NULL if @mod is 0.
  248. * @root: The root of the target tree.
  249. * @path: Path to use for the search.
  250. * @dir: The inode number (objectid) of the directory.
  251. * @index: The index number.
  252. * @name: The name associated to the directory entry we are looking for.
  253. * @name_len: The length of the name.
  254. * @mod: Used to indicate if the tree search is meant for a read only
  255. * lookup, for a modification lookup or for a deletion lookup, so
  256. * its value should be 0, 1 or -1, respectively.
  257. *
  258. * Returns: NULL if the dir index item does not exists, an error pointer if an
  259. * error happened, or a pointer to a dir item if the dir index item exists and
  260. * matches the criteria (name and index number).
  261. */
  262. struct btrfs_dir_item *
  263. btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
  264. struct btrfs_root *root,
  265. struct btrfs_path *path, u64 dir,
  266. u64 index, const struct fscrypt_str *name, int mod)
  267. {
  268. struct btrfs_dir_item *di;
  269. struct btrfs_key key;
  270. key.objectid = dir;
  271. key.type = BTRFS_DIR_INDEX_KEY;
  272. key.offset = index;
  273. di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
  274. name->len, mod);
  275. if (di == ERR_PTR(-ENOENT))
  276. return NULL;
  277. return di;
  278. }
  279. struct btrfs_dir_item *
  280. btrfs_search_dir_index_item(struct btrfs_root *root, struct btrfs_path *path,
  281. u64 dirid, const struct fscrypt_str *name)
  282. {
  283. struct btrfs_dir_item *di;
  284. struct btrfs_key key;
  285. int ret;
  286. key.objectid = dirid;
  287. key.type = BTRFS_DIR_INDEX_KEY;
  288. key.offset = 0;
  289. btrfs_for_each_slot(root, &key, &key, path, ret) {
  290. if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
  291. break;
  292. di = btrfs_match_dir_item_name(path, name->name, name->len);
  293. if (di)
  294. return di;
  295. }
  296. /* Adjust return code if the key was not found in the next leaf. */
  297. if (ret >= 0)
  298. ret = -ENOENT;
  299. return ERR_PTR(ret);
  300. }
  301. struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
  302. struct btrfs_root *root,
  303. struct btrfs_path *path, u64 dir,
  304. const char *name, u16 name_len,
  305. int mod)
  306. {
  307. struct btrfs_key key;
  308. struct btrfs_dir_item *di;
  309. key.objectid = dir;
  310. key.type = BTRFS_XATTR_ITEM_KEY;
  311. key.offset = btrfs_name_hash(name, name_len);
  312. di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
  313. if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
  314. return NULL;
  315. return di;
  316. }
  317. /*
  318. * helper function to look at the directory item pointed to by 'path'
  319. * this walks through all the entries in a dir item and finds one
  320. * for a specific name.
  321. */
  322. struct btrfs_dir_item *btrfs_match_dir_item_name(const struct btrfs_path *path,
  323. const char *name, int name_len)
  324. {
  325. struct btrfs_dir_item *dir_item;
  326. unsigned long name_ptr;
  327. u32 total_len;
  328. u32 cur = 0;
  329. u32 this_len;
  330. struct extent_buffer *leaf;
  331. leaf = path->nodes[0];
  332. dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
  333. total_len = btrfs_item_size(leaf, path->slots[0]);
  334. while (cur < total_len) {
  335. this_len = sizeof(*dir_item) +
  336. btrfs_dir_name_len(leaf, dir_item) +
  337. btrfs_dir_data_len(leaf, dir_item);
  338. name_ptr = (unsigned long)(dir_item + 1);
  339. if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
  340. memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
  341. return dir_item;
  342. cur += this_len;
  343. dir_item = (struct btrfs_dir_item *)((char *)dir_item +
  344. this_len);
  345. }
  346. return NULL;
  347. }
  348. /*
  349. * given a pointer into a directory item, delete it. This
  350. * handles items that have more than one entry in them.
  351. */
  352. int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
  353. struct btrfs_root *root,
  354. struct btrfs_path *path,
  355. const struct btrfs_dir_item *di)
  356. {
  357. struct extent_buffer *leaf;
  358. u32 sub_item_len;
  359. u32 item_len;
  360. int ret = 0;
  361. leaf = path->nodes[0];
  362. sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
  363. btrfs_dir_data_len(leaf, di);
  364. item_len = btrfs_item_size(leaf, path->slots[0]);
  365. if (sub_item_len == item_len) {
  366. ret = btrfs_del_item(trans, root, path);
  367. } else {
  368. /* MARKER */
  369. unsigned long ptr = (unsigned long)di;
  370. unsigned long start;
  371. start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  372. memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
  373. item_len - (ptr + sub_item_len - start));
  374. btrfs_truncate_item(trans, path, item_len - sub_item_len, 1);
  375. }
  376. return ret;
  377. }