dir-item.c 13 KB

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