dir-item.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
  79. return -ENOSPC;
  80. key.objectid = objectid;
  81. key.type = BTRFS_XATTR_ITEM_KEY;
  82. key.offset = btrfs_name_hash(name, name_len);
  83. data_size = sizeof(*dir_item) + name_len + data_len;
  84. dir_item = insert_with_overflow(trans, root, path, &key, data_size,
  85. name, name_len);
  86. if (IS_ERR(dir_item))
  87. return PTR_ERR(dir_item);
  88. memset(&location, 0, sizeof(location));
  89. leaf = path->nodes[0];
  90. btrfs_cpu_key_to_disk(&disk_key, &location);
  91. btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
  92. btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
  93. btrfs_set_dir_name_len(leaf, dir_item, name_len);
  94. btrfs_set_dir_transid(leaf, dir_item, trans->transid);
  95. btrfs_set_dir_data_len(leaf, dir_item, data_len);
  96. name_ptr = (unsigned long)(dir_item + 1);
  97. data_ptr = (unsigned long)((char *)name_ptr + name_len);
  98. write_extent_buffer(leaf, name, name_ptr, name_len);
  99. write_extent_buffer(leaf, data, data_ptr, data_len);
  100. btrfs_mark_buffer_dirty(path->nodes[0]);
  101. return ret;
  102. }
  103. /*
  104. * insert a directory item in the tree, doing all the magic for
  105. * both indexes. 'dir' indicates which objectid to insert it into,
  106. * 'location' is the key to stuff into the directory item, 'type' is the
  107. * type of the inode we're pointing to, and 'index' is the sequence number
  108. * to use for the second index (if one is created).
  109. * Will return 0 or -ENOMEM
  110. */
  111. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  112. *root, const char *name, int name_len,
  113. struct btrfs_inode *dir, struct btrfs_key *location,
  114. u8 type, u64 index)
  115. {
  116. int ret = 0;
  117. int ret2 = 0;
  118. struct btrfs_path *path;
  119. struct btrfs_dir_item *dir_item;
  120. struct extent_buffer *leaf;
  121. unsigned long name_ptr;
  122. struct btrfs_key key;
  123. struct btrfs_disk_key disk_key;
  124. u32 data_size;
  125. key.objectid = btrfs_ino(dir);
  126. key.type = BTRFS_DIR_ITEM_KEY;
  127. key.offset = btrfs_name_hash(name, name_len);
  128. path = btrfs_alloc_path();
  129. if (!path)
  130. return -ENOMEM;
  131. path->leave_spinning = 1;
  132. btrfs_cpu_key_to_disk(&disk_key, location);
  133. data_size = sizeof(*dir_item) + name_len;
  134. dir_item = insert_with_overflow(trans, root, path, &key, data_size,
  135. name, name_len);
  136. if (IS_ERR(dir_item)) {
  137. ret = PTR_ERR(dir_item);
  138. if (ret == -EEXIST)
  139. goto second_insert;
  140. goto out_free;
  141. }
  142. leaf = path->nodes[0];
  143. btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
  144. btrfs_set_dir_type(leaf, dir_item, type);
  145. btrfs_set_dir_data_len(leaf, dir_item, 0);
  146. btrfs_set_dir_name_len(leaf, dir_item, name_len);
  147. btrfs_set_dir_transid(leaf, dir_item, trans->transid);
  148. name_ptr = (unsigned long)(dir_item + 1);
  149. write_extent_buffer(leaf, name, name_ptr, name_len);
  150. btrfs_mark_buffer_dirty(leaf);
  151. second_insert:
  152. /* FIXME, use some real flag for selecting the extra index */
  153. if (root == root->fs_info->tree_root) {
  154. ret = 0;
  155. goto out_free;
  156. }
  157. btrfs_release_path(path);
  158. ret2 = btrfs_insert_delayed_dir_index(trans, root->fs_info, name,
  159. name_len, dir, &disk_key, type, 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. total_len = btrfs_item_size_nr(leaf, path->slots[0]);
  350. while (cur < total_len) {
  351. this_len = sizeof(*dir_item) +
  352. btrfs_dir_name_len(leaf, dir_item) +
  353. btrfs_dir_data_len(leaf, dir_item);
  354. name_ptr = (unsigned long)(dir_item + 1);
  355. if (verify_dir_item(fs_info, leaf, path->slots[0], dir_item))
  356. return NULL;
  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. int slot,
  400. struct btrfs_dir_item *dir_item)
  401. {
  402. u16 namelen = BTRFS_NAME_LEN;
  403. int ret;
  404. u8 type = btrfs_dir_type(leaf, dir_item);
  405. if (type >= BTRFS_FT_MAX) {
  406. btrfs_crit(fs_info, "invalid dir item type: %d", (int)type);
  407. return 1;
  408. }
  409. if (type == BTRFS_FT_XATTR)
  410. namelen = XATTR_NAME_MAX;
  411. if (btrfs_dir_name_len(leaf, dir_item) > namelen) {
  412. btrfs_crit(fs_info, "invalid dir item name len: %u",
  413. (unsigned)btrfs_dir_name_len(leaf, dir_item));
  414. return 1;
  415. }
  416. namelen = btrfs_dir_name_len(leaf, dir_item);
  417. ret = btrfs_is_name_len_valid(leaf, slot,
  418. (unsigned long)(dir_item + 1), namelen);
  419. if (!ret)
  420. return 1;
  421. /* BTRFS_MAX_XATTR_SIZE is the same for all dir items */
  422. if ((btrfs_dir_data_len(leaf, dir_item) +
  423. btrfs_dir_name_len(leaf, dir_item)) >
  424. BTRFS_MAX_XATTR_SIZE(fs_info)) {
  425. btrfs_crit(fs_info, "invalid dir item name + data len: %u + %u",
  426. (unsigned)btrfs_dir_name_len(leaf, dir_item),
  427. (unsigned)btrfs_dir_data_len(leaf, dir_item));
  428. return 1;
  429. }
  430. return 0;
  431. }
  432. bool btrfs_is_name_len_valid(struct extent_buffer *leaf, int slot,
  433. unsigned long start, u16 name_len)
  434. {
  435. struct btrfs_fs_info *fs_info = leaf->fs_info;
  436. struct btrfs_key key;
  437. u32 read_start;
  438. u32 read_end;
  439. u32 item_start;
  440. u32 item_end;
  441. u32 size;
  442. bool ret = true;
  443. ASSERT(start > BTRFS_LEAF_DATA_OFFSET);
  444. read_start = start - BTRFS_LEAF_DATA_OFFSET;
  445. read_end = read_start + name_len;
  446. item_start = btrfs_item_offset_nr(leaf, slot);
  447. item_end = btrfs_item_end_nr(leaf, slot);
  448. btrfs_item_key_to_cpu(leaf, &key, slot);
  449. switch (key.type) {
  450. case BTRFS_DIR_ITEM_KEY:
  451. case BTRFS_XATTR_ITEM_KEY:
  452. case BTRFS_DIR_INDEX_KEY:
  453. size = sizeof(struct btrfs_dir_item);
  454. break;
  455. case BTRFS_INODE_REF_KEY:
  456. size = sizeof(struct btrfs_inode_ref);
  457. break;
  458. case BTRFS_INODE_EXTREF_KEY:
  459. size = sizeof(struct btrfs_inode_extref);
  460. break;
  461. case BTRFS_ROOT_REF_KEY:
  462. case BTRFS_ROOT_BACKREF_KEY:
  463. size = sizeof(struct btrfs_root_ref);
  464. break;
  465. default:
  466. ret = false;
  467. goto out;
  468. }
  469. if (read_start < item_start) {
  470. ret = false;
  471. goto out;
  472. }
  473. if (read_end > item_end) {
  474. ret = false;
  475. goto out;
  476. }
  477. /* there shall be item(s) before name */
  478. if (read_start - item_start < size) {
  479. ret = false;
  480. goto out;
  481. }
  482. out:
  483. if (!ret)
  484. btrfs_crit(fs_info, "invalid dir item name len: %u",
  485. (unsigned int)name_len);
  486. return ret;
  487. }