inode-item.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include "ctree.h"
  6. #include "inode-item.h"
  7. #include "disk-io.h"
  8. #include "transaction.h"
  9. #include "print-tree.h"
  10. struct btrfs_inode_ref *btrfs_find_name_in_backref(struct extent_buffer *leaf,
  11. int slot,
  12. const struct fscrypt_str *name)
  13. {
  14. struct btrfs_inode_ref *ref;
  15. unsigned long ptr;
  16. unsigned long name_ptr;
  17. u32 item_size;
  18. u32 cur_offset = 0;
  19. int len;
  20. item_size = btrfs_item_size(leaf, slot);
  21. ptr = btrfs_item_ptr_offset(leaf, slot);
  22. while (cur_offset < item_size) {
  23. ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  24. len = btrfs_inode_ref_name_len(leaf, ref);
  25. name_ptr = (unsigned long)(ref + 1);
  26. cur_offset += len + sizeof(*ref);
  27. if (len != name->len)
  28. continue;
  29. if (memcmp_extent_buffer(leaf, name->name, name_ptr,
  30. name->len) == 0)
  31. return ref;
  32. }
  33. return NULL;
  34. }
  35. struct btrfs_inode_extref *btrfs_find_name_in_ext_backref(
  36. struct extent_buffer *leaf, int slot, u64 ref_objectid,
  37. const struct fscrypt_str *name)
  38. {
  39. struct btrfs_inode_extref *extref;
  40. unsigned long ptr;
  41. unsigned long name_ptr;
  42. u32 item_size;
  43. u32 cur_offset = 0;
  44. int ref_name_len;
  45. item_size = btrfs_item_size(leaf, slot);
  46. ptr = btrfs_item_ptr_offset(leaf, slot);
  47. /*
  48. * Search all extended backrefs in this item. We're only
  49. * looking through any collisions so most of the time this is
  50. * just going to compare against one buffer. If all is well,
  51. * we'll return success and the inode ref object.
  52. */
  53. while (cur_offset < item_size) {
  54. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  55. name_ptr = (unsigned long)(&extref->name);
  56. ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
  57. if (ref_name_len == name->len &&
  58. btrfs_inode_extref_parent(leaf, extref) == ref_objectid &&
  59. (memcmp_extent_buffer(leaf, name->name, name_ptr,
  60. name->len) == 0))
  61. return extref;
  62. cur_offset += ref_name_len + sizeof(*extref);
  63. }
  64. return NULL;
  65. }
  66. /* Returns NULL if no extref found */
  67. struct btrfs_inode_extref *
  68. btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
  69. struct btrfs_root *root,
  70. struct btrfs_path *path,
  71. const struct fscrypt_str *name,
  72. u64 inode_objectid, u64 ref_objectid, int ins_len,
  73. int cow)
  74. {
  75. int ret;
  76. struct btrfs_key key;
  77. key.objectid = inode_objectid;
  78. key.type = BTRFS_INODE_EXTREF_KEY;
  79. key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
  80. ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
  81. if (ret < 0)
  82. return ERR_PTR(ret);
  83. if (ret > 0)
  84. return NULL;
  85. return btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
  86. ref_objectid, name);
  87. }
  88. static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
  89. struct btrfs_root *root,
  90. const struct fscrypt_str *name,
  91. u64 inode_objectid, u64 ref_objectid,
  92. u64 *index)
  93. {
  94. struct btrfs_path *path;
  95. struct btrfs_key key;
  96. struct btrfs_inode_extref *extref;
  97. struct extent_buffer *leaf;
  98. int ret;
  99. int del_len = name->len + sizeof(*extref);
  100. unsigned long ptr;
  101. unsigned long item_start;
  102. u32 item_size;
  103. key.objectid = inode_objectid;
  104. key.type = BTRFS_INODE_EXTREF_KEY;
  105. key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
  106. path = btrfs_alloc_path();
  107. if (!path)
  108. return -ENOMEM;
  109. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  110. if (ret > 0)
  111. ret = -ENOENT;
  112. if (ret < 0)
  113. goto out;
  114. /*
  115. * Sanity check - did we find the right item for this name?
  116. * This should always succeed so error here will make the FS
  117. * readonly.
  118. */
  119. extref = btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
  120. ref_objectid, name);
  121. if (!extref) {
  122. btrfs_handle_fs_error(root->fs_info, -ENOENT, NULL);
  123. ret = -EROFS;
  124. goto out;
  125. }
  126. leaf = path->nodes[0];
  127. item_size = btrfs_item_size(leaf, path->slots[0]);
  128. if (index)
  129. *index = btrfs_inode_extref_index(leaf, extref);
  130. if (del_len == item_size) {
  131. /*
  132. * Common case only one ref in the item, remove the
  133. * whole item.
  134. */
  135. ret = btrfs_del_item(trans, root, path);
  136. goto out;
  137. }
  138. ptr = (unsigned long)extref;
  139. item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  140. memmove_extent_buffer(leaf, ptr, ptr + del_len,
  141. item_size - (ptr + del_len - item_start));
  142. btrfs_truncate_item(path, item_size - del_len, 1);
  143. out:
  144. btrfs_free_path(path);
  145. return ret;
  146. }
  147. int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
  148. struct btrfs_root *root, const struct fscrypt_str *name,
  149. u64 inode_objectid, u64 ref_objectid, u64 *index)
  150. {
  151. struct btrfs_path *path;
  152. struct btrfs_key key;
  153. struct btrfs_inode_ref *ref;
  154. struct extent_buffer *leaf;
  155. unsigned long ptr;
  156. unsigned long item_start;
  157. u32 item_size;
  158. u32 sub_item_len;
  159. int ret;
  160. int search_ext_refs = 0;
  161. int del_len = name->len + sizeof(*ref);
  162. key.objectid = inode_objectid;
  163. key.offset = ref_objectid;
  164. key.type = BTRFS_INODE_REF_KEY;
  165. path = btrfs_alloc_path();
  166. if (!path)
  167. return -ENOMEM;
  168. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  169. if (ret > 0) {
  170. ret = -ENOENT;
  171. search_ext_refs = 1;
  172. goto out;
  173. } else if (ret < 0) {
  174. goto out;
  175. }
  176. ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], name);
  177. if (!ref) {
  178. ret = -ENOENT;
  179. search_ext_refs = 1;
  180. goto out;
  181. }
  182. leaf = path->nodes[0];
  183. item_size = btrfs_item_size(leaf, path->slots[0]);
  184. if (index)
  185. *index = btrfs_inode_ref_index(leaf, ref);
  186. if (del_len == item_size) {
  187. ret = btrfs_del_item(trans, root, path);
  188. goto out;
  189. }
  190. ptr = (unsigned long)ref;
  191. sub_item_len = name->len + sizeof(*ref);
  192. item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  193. memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
  194. item_size - (ptr + sub_item_len - item_start));
  195. btrfs_truncate_item(path, item_size - sub_item_len, 1);
  196. out:
  197. btrfs_free_path(path);
  198. if (search_ext_refs) {
  199. /*
  200. * No refs were found, or we could not find the
  201. * name in our ref array. Find and remove the extended
  202. * inode ref then.
  203. */
  204. return btrfs_del_inode_extref(trans, root, name,
  205. inode_objectid, ref_objectid, index);
  206. }
  207. return ret;
  208. }
  209. /*
  210. * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree.
  211. *
  212. * The caller must have checked against BTRFS_LINK_MAX already.
  213. */
  214. static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
  215. struct btrfs_root *root,
  216. const struct fscrypt_str *name,
  217. u64 inode_objectid, u64 ref_objectid,
  218. u64 index)
  219. {
  220. struct btrfs_inode_extref *extref;
  221. int ret;
  222. int ins_len = name->len + sizeof(*extref);
  223. unsigned long ptr;
  224. struct btrfs_path *path;
  225. struct btrfs_key key;
  226. struct extent_buffer *leaf;
  227. key.objectid = inode_objectid;
  228. key.type = BTRFS_INODE_EXTREF_KEY;
  229. key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);
  230. path = btrfs_alloc_path();
  231. if (!path)
  232. return -ENOMEM;
  233. ret = btrfs_insert_empty_item(trans, root, path, &key,
  234. ins_len);
  235. if (ret == -EEXIST) {
  236. if (btrfs_find_name_in_ext_backref(path->nodes[0],
  237. path->slots[0],
  238. ref_objectid,
  239. name))
  240. goto out;
  241. btrfs_extend_item(path, ins_len);
  242. ret = 0;
  243. }
  244. if (ret < 0)
  245. goto out;
  246. leaf = path->nodes[0];
  247. ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char);
  248. ptr += btrfs_item_size(leaf, path->slots[0]) - ins_len;
  249. extref = (struct btrfs_inode_extref *)ptr;
  250. btrfs_set_inode_extref_name_len(path->nodes[0], extref, name->len);
  251. btrfs_set_inode_extref_index(path->nodes[0], extref, index);
  252. btrfs_set_inode_extref_parent(path->nodes[0], extref, ref_objectid);
  253. ptr = (unsigned long)&extref->name;
  254. write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
  255. btrfs_mark_buffer_dirty(path->nodes[0]);
  256. out:
  257. btrfs_free_path(path);
  258. return ret;
  259. }
  260. /* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */
  261. int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
  262. struct btrfs_root *root, const struct fscrypt_str *name,
  263. u64 inode_objectid, u64 ref_objectid, u64 index)
  264. {
  265. struct btrfs_fs_info *fs_info = root->fs_info;
  266. struct btrfs_path *path;
  267. struct btrfs_key key;
  268. struct btrfs_inode_ref *ref;
  269. unsigned long ptr;
  270. int ret;
  271. int ins_len = name->len + sizeof(*ref);
  272. key.objectid = inode_objectid;
  273. key.offset = ref_objectid;
  274. key.type = BTRFS_INODE_REF_KEY;
  275. path = btrfs_alloc_path();
  276. if (!path)
  277. return -ENOMEM;
  278. path->skip_release_on_error = 1;
  279. ret = btrfs_insert_empty_item(trans, root, path, &key,
  280. ins_len);
  281. if (ret == -EEXIST) {
  282. u32 old_size;
  283. ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
  284. name);
  285. if (ref)
  286. goto out;
  287. old_size = btrfs_item_size(path->nodes[0], path->slots[0]);
  288. btrfs_extend_item(path, ins_len);
  289. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  290. struct btrfs_inode_ref);
  291. ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
  292. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name->len);
  293. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  294. ptr = (unsigned long)(ref + 1);
  295. ret = 0;
  296. } else if (ret < 0) {
  297. if (ret == -EOVERFLOW) {
  298. if (btrfs_find_name_in_backref(path->nodes[0],
  299. path->slots[0],
  300. name))
  301. ret = -EEXIST;
  302. else
  303. ret = -EMLINK;
  304. }
  305. goto out;
  306. } else {
  307. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  308. struct btrfs_inode_ref);
  309. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name->len);
  310. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  311. ptr = (unsigned long)(ref + 1);
  312. }
  313. write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
  314. btrfs_mark_buffer_dirty(path->nodes[0]);
  315. out:
  316. btrfs_free_path(path);
  317. if (ret == -EMLINK) {
  318. struct btrfs_super_block *disk_super = fs_info->super_copy;
  319. /* We ran out of space in the ref array. Need to
  320. * add an extended ref. */
  321. if (btrfs_super_incompat_flags(disk_super)
  322. & BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
  323. ret = btrfs_insert_inode_extref(trans, root, name,
  324. inode_objectid,
  325. ref_objectid, index);
  326. }
  327. return ret;
  328. }
  329. int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
  330. struct btrfs_root *root,
  331. struct btrfs_path *path, u64 objectid)
  332. {
  333. struct btrfs_key key;
  334. int ret;
  335. key.objectid = objectid;
  336. key.type = BTRFS_INODE_ITEM_KEY;
  337. key.offset = 0;
  338. ret = btrfs_insert_empty_item(trans, root, path, &key,
  339. sizeof(struct btrfs_inode_item));
  340. return ret;
  341. }
  342. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  343. *root, struct btrfs_path *path,
  344. struct btrfs_key *location, int mod)
  345. {
  346. int ins_len = mod < 0 ? -1 : 0;
  347. int cow = mod != 0;
  348. int ret;
  349. int slot;
  350. struct extent_buffer *leaf;
  351. struct btrfs_key found_key;
  352. ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
  353. if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY &&
  354. location->offset == (u64)-1 && path->slots[0] != 0) {
  355. slot = path->slots[0] - 1;
  356. leaf = path->nodes[0];
  357. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  358. if (found_key.objectid == location->objectid &&
  359. found_key.type == location->type) {
  360. path->slots[0]--;
  361. return 0;
  362. }
  363. }
  364. return ret;
  365. }
  366. static inline void btrfs_trace_truncate(struct btrfs_inode *inode,
  367. struct extent_buffer *leaf,
  368. struct btrfs_file_extent_item *fi,
  369. u64 offset, int extent_type, int slot)
  370. {
  371. if (!inode)
  372. return;
  373. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  374. trace_btrfs_truncate_show_fi_inline(inode, leaf, fi, slot,
  375. offset);
  376. else
  377. trace_btrfs_truncate_show_fi_regular(inode, leaf, fi, offset);
  378. }
  379. /*
  380. * Remove inode items from a given root.
  381. *
  382. * @trans: A transaction handle.
  383. * @root: The root from which to remove items.
  384. * @inode: The inode whose items we want to remove.
  385. * @control: The btrfs_truncate_control to control how and what we
  386. * are truncating.
  387. *
  388. * Remove all keys associated with the inode from the given root that have a key
  389. * with a type greater than or equals to @min_type. When @min_type has a value of
  390. * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
  391. * greater than or equals to @new_size. If a file extent item that starts before
  392. * @new_size and ends after it is found, its length is adjusted.
  393. *
  394. * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
  395. * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
  396. */
  397. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  398. struct btrfs_root *root,
  399. struct btrfs_truncate_control *control)
  400. {
  401. struct btrfs_fs_info *fs_info = root->fs_info;
  402. struct btrfs_path *path;
  403. struct extent_buffer *leaf;
  404. struct btrfs_file_extent_item *fi;
  405. struct btrfs_key key;
  406. struct btrfs_key found_key;
  407. u64 new_size = control->new_size;
  408. u64 extent_num_bytes = 0;
  409. u64 extent_offset = 0;
  410. u64 item_end = 0;
  411. u32 found_type = (u8)-1;
  412. int del_item;
  413. int pending_del_nr = 0;
  414. int pending_del_slot = 0;
  415. int extent_type = -1;
  416. int ret;
  417. u64 bytes_deleted = 0;
  418. bool be_nice = false;
  419. ASSERT(control->inode || !control->clear_extent_range);
  420. ASSERT(new_size == 0 || control->min_type == BTRFS_EXTENT_DATA_KEY);
  421. control->last_size = new_size;
  422. control->sub_bytes = 0;
  423. /*
  424. * For shareable roots we want to back off from time to time, this turns
  425. * out to be subvolume roots, reloc roots, and data reloc roots.
  426. */
  427. if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  428. be_nice = true;
  429. path = btrfs_alloc_path();
  430. if (!path)
  431. return -ENOMEM;
  432. path->reada = READA_BACK;
  433. key.objectid = control->ino;
  434. key.offset = (u64)-1;
  435. key.type = (u8)-1;
  436. search_again:
  437. /*
  438. * With a 16K leaf size and 128MiB extents, you can actually queue up a
  439. * huge file in a single leaf. Most of the time that bytes_deleted is
  440. * > 0, it will be huge by the time we get here
  441. */
  442. if (be_nice && bytes_deleted > SZ_32M &&
  443. btrfs_should_end_transaction(trans)) {
  444. ret = -EAGAIN;
  445. goto out;
  446. }
  447. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  448. if (ret < 0)
  449. goto out;
  450. if (ret > 0) {
  451. ret = 0;
  452. /* There are no items in the tree for us to truncate, we're done */
  453. if (path->slots[0] == 0)
  454. goto out;
  455. path->slots[0]--;
  456. }
  457. while (1) {
  458. u64 clear_start = 0, clear_len = 0, extent_start = 0;
  459. bool should_throttle = false;
  460. fi = NULL;
  461. leaf = path->nodes[0];
  462. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  463. found_type = found_key.type;
  464. if (found_key.objectid != control->ino)
  465. break;
  466. if (found_type < control->min_type)
  467. break;
  468. item_end = found_key.offset;
  469. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  470. fi = btrfs_item_ptr(leaf, path->slots[0],
  471. struct btrfs_file_extent_item);
  472. extent_type = btrfs_file_extent_type(leaf, fi);
  473. if (extent_type != BTRFS_FILE_EXTENT_INLINE)
  474. item_end +=
  475. btrfs_file_extent_num_bytes(leaf, fi);
  476. else if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  477. item_end += btrfs_file_extent_ram_bytes(leaf, fi);
  478. btrfs_trace_truncate(control->inode, leaf, fi,
  479. found_key.offset, extent_type,
  480. path->slots[0]);
  481. item_end--;
  482. }
  483. if (found_type > control->min_type) {
  484. del_item = 1;
  485. } else {
  486. if (item_end < new_size)
  487. break;
  488. if (found_key.offset >= new_size)
  489. del_item = 1;
  490. else
  491. del_item = 0;
  492. }
  493. /* FIXME, shrink the extent if the ref count is only 1 */
  494. if (found_type != BTRFS_EXTENT_DATA_KEY)
  495. goto delete;
  496. control->extents_found++;
  497. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  498. u64 num_dec;
  499. clear_start = found_key.offset;
  500. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  501. if (!del_item) {
  502. u64 orig_num_bytes =
  503. btrfs_file_extent_num_bytes(leaf, fi);
  504. extent_num_bytes = ALIGN(new_size -
  505. found_key.offset,
  506. fs_info->sectorsize);
  507. clear_start = ALIGN(new_size, fs_info->sectorsize);
  508. btrfs_set_file_extent_num_bytes(leaf, fi,
  509. extent_num_bytes);
  510. num_dec = (orig_num_bytes - extent_num_bytes);
  511. if (extent_start != 0)
  512. control->sub_bytes += num_dec;
  513. btrfs_mark_buffer_dirty(leaf);
  514. } else {
  515. extent_num_bytes =
  516. btrfs_file_extent_disk_num_bytes(leaf, fi);
  517. extent_offset = found_key.offset -
  518. btrfs_file_extent_offset(leaf, fi);
  519. /* FIXME blocksize != 4096 */
  520. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  521. if (extent_start != 0)
  522. control->sub_bytes += num_dec;
  523. }
  524. clear_len = num_dec;
  525. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  526. /*
  527. * We can't truncate inline items that have had
  528. * special encodings
  529. */
  530. if (!del_item &&
  531. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  532. btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
  533. btrfs_file_extent_compression(leaf, fi) == 0) {
  534. u32 size = (u32)(new_size - found_key.offset);
  535. btrfs_set_file_extent_ram_bytes(leaf, fi, size);
  536. size = btrfs_file_extent_calc_inline_size(size);
  537. btrfs_truncate_item(path, size, 1);
  538. } else if (!del_item) {
  539. /*
  540. * We have to bail so the last_size is set to
  541. * just before this extent.
  542. */
  543. ret = BTRFS_NEED_TRUNCATE_BLOCK;
  544. break;
  545. } else {
  546. /*
  547. * Inline extents are special, we just treat
  548. * them as a full sector worth in the file
  549. * extent tree just for simplicity sake.
  550. */
  551. clear_len = fs_info->sectorsize;
  552. }
  553. control->sub_bytes += item_end + 1 - new_size;
  554. }
  555. delete:
  556. /*
  557. * We only want to clear the file extent range if we're
  558. * modifying the actual inode's mapping, which is just the
  559. * normal truncate path.
  560. */
  561. if (control->clear_extent_range) {
  562. ret = btrfs_inode_clear_file_extent_range(control->inode,
  563. clear_start, clear_len);
  564. if (ret) {
  565. btrfs_abort_transaction(trans, ret);
  566. break;
  567. }
  568. }
  569. if (del_item) {
  570. ASSERT(!pending_del_nr ||
  571. ((path->slots[0] + 1) == pending_del_slot));
  572. control->last_size = found_key.offset;
  573. if (!pending_del_nr) {
  574. /* No pending yet, add ourselves */
  575. pending_del_slot = path->slots[0];
  576. pending_del_nr = 1;
  577. } else if (pending_del_nr &&
  578. path->slots[0] + 1 == pending_del_slot) {
  579. /* Hop on the pending chunk */
  580. pending_del_nr++;
  581. pending_del_slot = path->slots[0];
  582. }
  583. } else {
  584. control->last_size = new_size;
  585. break;
  586. }
  587. if (del_item && extent_start != 0 && !control->skip_ref_updates) {
  588. struct btrfs_ref ref = { 0 };
  589. bytes_deleted += extent_num_bytes;
  590. btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
  591. extent_start, extent_num_bytes, 0);
  592. btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
  593. control->ino, extent_offset,
  594. root->root_key.objectid, false);
  595. ret = btrfs_free_extent(trans, &ref);
  596. if (ret) {
  597. btrfs_abort_transaction(trans, ret);
  598. break;
  599. }
  600. if (be_nice) {
  601. if (btrfs_should_throttle_delayed_refs(trans))
  602. should_throttle = true;
  603. }
  604. }
  605. if (found_type == BTRFS_INODE_ITEM_KEY)
  606. break;
  607. if (path->slots[0] == 0 ||
  608. path->slots[0] != pending_del_slot ||
  609. should_throttle) {
  610. if (pending_del_nr) {
  611. ret = btrfs_del_items(trans, root, path,
  612. pending_del_slot,
  613. pending_del_nr);
  614. if (ret) {
  615. btrfs_abort_transaction(trans, ret);
  616. break;
  617. }
  618. pending_del_nr = 0;
  619. }
  620. btrfs_release_path(path);
  621. /*
  622. * We can generate a lot of delayed refs, so we need to
  623. * throttle every once and a while and make sure we're
  624. * adding enough space to keep up with the work we are
  625. * generating. Since we hold a transaction here we
  626. * can't flush, and we don't want to FLUSH_LIMIT because
  627. * we could have generated too many delayed refs to
  628. * actually allocate, so just bail if we're short and
  629. * let the normal reservation dance happen higher up.
  630. */
  631. if (should_throttle) {
  632. ret = btrfs_delayed_refs_rsv_refill(fs_info,
  633. BTRFS_RESERVE_NO_FLUSH);
  634. if (ret) {
  635. ret = -EAGAIN;
  636. break;
  637. }
  638. }
  639. goto search_again;
  640. } else {
  641. path->slots[0]--;
  642. }
  643. }
  644. out:
  645. if (ret >= 0 && pending_del_nr) {
  646. int err;
  647. err = btrfs_del_items(trans, root, path, pending_del_slot,
  648. pending_del_nr);
  649. if (err) {
  650. btrfs_abort_transaction(trans, err);
  651. ret = err;
  652. }
  653. }
  654. ASSERT(control->last_size >= new_size);
  655. if (!ret && control->last_size > new_size)
  656. control->last_size = new_size;
  657. btrfs_free_path(path);
  658. return ret;
  659. }