dir-item.c 13 KB

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