inode-item.c 20 KB

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