dir-item.c 11 KB

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