space-info.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "misc.h"
  3. #include "ctree.h"
  4. #include "space-info.h"
  5. #include "sysfs.h"
  6. #include "volumes.h"
  7. #include "free-space-cache.h"
  8. #include "ordered-data.h"
  9. #include "transaction.h"
  10. #include "block-group.h"
  11. /*
  12. * HOW DOES SPACE RESERVATION WORK
  13. *
  14. * If you want to know about delalloc specifically, there is a separate comment
  15. * for that with the delalloc code. This comment is about how the whole system
  16. * works generally.
  17. *
  18. * BASIC CONCEPTS
  19. *
  20. * 1) space_info. This is the ultimate arbiter of how much space we can use.
  21. * There's a description of the bytes_ fields with the struct declaration,
  22. * refer to that for specifics on each field. Suffice it to say that for
  23. * reservations we care about total_bytes - SUM(space_info->bytes_) when
  24. * determining if there is space to make an allocation. There is a space_info
  25. * for METADATA, SYSTEM, and DATA areas.
  26. *
  27. * 2) block_rsv's. These are basically buckets for every different type of
  28. * metadata reservation we have. You can see the comment in the block_rsv
  29. * code on the rules for each type, but generally block_rsv->reserved is how
  30. * much space is accounted for in space_info->bytes_may_use.
  31. *
  32. * 3) btrfs_calc*_size. These are the worst case calculations we used based
  33. * on the number of items we will want to modify. We have one for changing
  34. * items, and one for inserting new items. Generally we use these helpers to
  35. * determine the size of the block reserves, and then use the actual bytes
  36. * values to adjust the space_info counters.
  37. *
  38. * MAKING RESERVATIONS, THE NORMAL CASE
  39. *
  40. * We call into either btrfs_reserve_data_bytes() or
  41. * btrfs_reserve_metadata_bytes(), depending on which we're looking for, with
  42. * num_bytes we want to reserve.
  43. *
  44. * ->reserve
  45. * space_info->bytes_may_reserve += num_bytes
  46. *
  47. * ->extent allocation
  48. * Call btrfs_add_reserved_bytes() which does
  49. * space_info->bytes_may_reserve -= num_bytes
  50. * space_info->bytes_reserved += extent_bytes
  51. *
  52. * ->insert reference
  53. * Call btrfs_update_block_group() which does
  54. * space_info->bytes_reserved -= extent_bytes
  55. * space_info->bytes_used += extent_bytes
  56. *
  57. * MAKING RESERVATIONS, FLUSHING NORMALLY (non-priority)
  58. *
  59. * Assume we are unable to simply make the reservation because we do not have
  60. * enough space
  61. *
  62. * -> __reserve_bytes
  63. * create a reserve_ticket with ->bytes set to our reservation, add it to
  64. * the tail of space_info->tickets, kick async flush thread
  65. *
  66. * ->handle_reserve_ticket
  67. * wait on ticket->wait for ->bytes to be reduced to 0, or ->error to be set
  68. * on the ticket.
  69. *
  70. * -> btrfs_async_reclaim_metadata_space/btrfs_async_reclaim_data_space
  71. * Flushes various things attempting to free up space.
  72. *
  73. * -> btrfs_try_granting_tickets()
  74. * This is called by anything that either subtracts space from
  75. * space_info->bytes_may_use, ->bytes_pinned, etc, or adds to the
  76. * space_info->total_bytes. This loops through the ->priority_tickets and
  77. * then the ->tickets list checking to see if the reservation can be
  78. * completed. If it can the space is added to space_info->bytes_may_use and
  79. * the ticket is woken up.
  80. *
  81. * -> ticket wakeup
  82. * Check if ->bytes == 0, if it does we got our reservation and we can carry
  83. * on, if not return the appropriate error (ENOSPC, but can be EINTR if we
  84. * were interrupted.)
  85. *
  86. * MAKING RESERVATIONS, FLUSHING HIGH PRIORITY
  87. *
  88. * Same as the above, except we add ourselves to the
  89. * space_info->priority_tickets, and we do not use ticket->wait, we simply
  90. * call flush_space() ourselves for the states that are safe for us to call
  91. * without deadlocking and hope for the best.
  92. *
  93. * THE FLUSHING STATES
  94. *
  95. * Generally speaking we will have two cases for each state, a "nice" state
  96. * and a "ALL THE THINGS" state. In btrfs we delay a lot of work in order to
  97. * reduce the locking over head on the various trees, and even to keep from
  98. * doing any work at all in the case of delayed refs. Each of these delayed
  99. * things however hold reservations, and so letting them run allows us to
  100. * reclaim space so we can make new reservations.
  101. *
  102. * FLUSH_DELAYED_ITEMS
  103. * Every inode has a delayed item to update the inode. Take a simple write
  104. * for example, we would update the inode item at write time to update the
  105. * mtime, and then again at finish_ordered_io() time in order to update the
  106. * isize or bytes. We keep these delayed items to coalesce these operations
  107. * into a single operation done on demand. These are an easy way to reclaim
  108. * metadata space.
  109. *
  110. * FLUSH_DELALLOC
  111. * Look at the delalloc comment to get an idea of how much space is reserved
  112. * for delayed allocation. We can reclaim some of this space simply by
  113. * running delalloc, but usually we need to wait for ordered extents to
  114. * reclaim the bulk of this space.
  115. *
  116. * FLUSH_DELAYED_REFS
  117. * We have a block reserve for the outstanding delayed refs space, and every
  118. * delayed ref operation holds a reservation. Running these is a quick way
  119. * to reclaim space, but we want to hold this until the end because COW can
  120. * churn a lot and we can avoid making some extent tree modifications if we
  121. * are able to delay for as long as possible.
  122. *
  123. * ALLOC_CHUNK
  124. * We will skip this the first time through space reservation, because of
  125. * overcommit and we don't want to have a lot of useless metadata space when
  126. * our worst case reservations will likely never come true.
  127. *
  128. * RUN_DELAYED_IPUTS
  129. * If we're freeing inodes we're likely freeing checksums, file extent
  130. * items, and extent tree items. Loads of space could be freed up by these
  131. * operations, however they won't be usable until the transaction commits.
  132. *
  133. * COMMIT_TRANS
  134. * may_commit_transaction() is the ultimate arbiter on whether we commit the
  135. * transaction or not. In order to avoid constantly churning we do all the
  136. * above flushing first and then commit the transaction as the last resort.
  137. * However we need to take into account things like pinned space that would
  138. * be freed, plus any delayed work we may not have gotten rid of in the case
  139. * of metadata.
  140. *
  141. * OVERCOMMIT
  142. *
  143. * Because we hold so many reservations for metadata we will allow you to
  144. * reserve more space than is currently free in the currently allocate
  145. * metadata space. This only happens with metadata, data does not allow
  146. * overcommitting.
  147. *
  148. * You can see the current logic for when we allow overcommit in
  149. * btrfs_can_overcommit(), but it only applies to unallocated space. If there
  150. * is no unallocated space to be had, all reservations are kept within the
  151. * free space in the allocated metadata chunks.
  152. *
  153. * Because of overcommitting, you generally want to use the
  154. * btrfs_can_overcommit() logic for metadata allocations, as it does the right
  155. * thing with or without extra unallocated space.
  156. */
  157. u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
  158. bool may_use_included)
  159. {
  160. ASSERT(s_info);
  161. return s_info->bytes_used + s_info->bytes_reserved +
  162. s_info->bytes_pinned + s_info->bytes_readonly +
  163. (may_use_included ? s_info->bytes_may_use : 0);
  164. }
  165. /*
  166. * after adding space to the filesystem, we need to clear the full flags
  167. * on all the space infos.
  168. */
  169. void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
  170. {
  171. struct list_head *head = &info->space_info;
  172. struct btrfs_space_info *found;
  173. list_for_each_entry(found, head, list)
  174. found->full = 0;
  175. }
  176. static int create_space_info(struct btrfs_fs_info *info, u64 flags)
  177. {
  178. struct btrfs_space_info *space_info;
  179. int i;
  180. int ret;
  181. space_info = kzalloc(sizeof(*space_info), GFP_NOFS);
  182. if (!space_info)
  183. return -ENOMEM;
  184. ret = percpu_counter_init(&space_info->total_bytes_pinned, 0,
  185. GFP_KERNEL);
  186. if (ret) {
  187. kfree(space_info);
  188. return ret;
  189. }
  190. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
  191. INIT_LIST_HEAD(&space_info->block_groups[i]);
  192. init_rwsem(&space_info->groups_sem);
  193. spin_lock_init(&space_info->lock);
  194. space_info->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  195. space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
  196. INIT_LIST_HEAD(&space_info->ro_bgs);
  197. INIT_LIST_HEAD(&space_info->tickets);
  198. INIT_LIST_HEAD(&space_info->priority_tickets);
  199. ret = btrfs_sysfs_add_space_info_type(info, space_info);
  200. if (ret)
  201. return ret;
  202. list_add(&space_info->list, &info->space_info);
  203. if (flags & BTRFS_BLOCK_GROUP_DATA)
  204. info->data_sinfo = space_info;
  205. return ret;
  206. }
  207. int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
  208. {
  209. struct btrfs_super_block *disk_super;
  210. u64 features;
  211. u64 flags;
  212. int mixed = 0;
  213. int ret;
  214. disk_super = fs_info->super_copy;
  215. if (!btrfs_super_root(disk_super))
  216. return -EINVAL;
  217. features = btrfs_super_incompat_flags(disk_super);
  218. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  219. mixed = 1;
  220. flags = BTRFS_BLOCK_GROUP_SYSTEM;
  221. ret = create_space_info(fs_info, flags);
  222. if (ret)
  223. goto out;
  224. if (mixed) {
  225. flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
  226. ret = create_space_info(fs_info, flags);
  227. } else {
  228. flags = BTRFS_BLOCK_GROUP_METADATA;
  229. ret = create_space_info(fs_info, flags);
  230. if (ret)
  231. goto out;
  232. flags = BTRFS_BLOCK_GROUP_DATA;
  233. ret = create_space_info(fs_info, flags);
  234. }
  235. out:
  236. return ret;
  237. }
  238. void btrfs_update_space_info(struct btrfs_fs_info *info, u64 flags,
  239. u64 total_bytes, u64 bytes_used,
  240. u64 bytes_readonly,
  241. struct btrfs_space_info **space_info)
  242. {
  243. struct btrfs_space_info *found;
  244. int factor;
  245. factor = btrfs_bg_type_to_factor(flags);
  246. found = btrfs_find_space_info(info, flags);
  247. ASSERT(found);
  248. spin_lock(&found->lock);
  249. found->total_bytes += total_bytes;
  250. found->disk_total += total_bytes * factor;
  251. found->bytes_used += bytes_used;
  252. found->disk_used += bytes_used * factor;
  253. found->bytes_readonly += bytes_readonly;
  254. if (total_bytes > 0)
  255. found->full = 0;
  256. btrfs_try_granting_tickets(info, found);
  257. spin_unlock(&found->lock);
  258. *space_info = found;
  259. }
  260. struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
  261. u64 flags)
  262. {
  263. struct list_head *head = &info->space_info;
  264. struct btrfs_space_info *found;
  265. flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
  266. list_for_each_entry(found, head, list) {
  267. if (found->flags & flags)
  268. return found;
  269. }
  270. return NULL;
  271. }
  272. static u64 calc_available_free_space(struct btrfs_fs_info *fs_info,
  273. struct btrfs_space_info *space_info,
  274. enum btrfs_reserve_flush_enum flush)
  275. {
  276. u64 profile;
  277. u64 avail;
  278. int factor;
  279. if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
  280. profile = btrfs_system_alloc_profile(fs_info);
  281. else
  282. profile = btrfs_metadata_alloc_profile(fs_info);
  283. avail = atomic64_read(&fs_info->free_chunk_space);
  284. /*
  285. * If we have dup, raid1 or raid10 then only half of the free
  286. * space is actually usable. For raid56, the space info used
  287. * doesn't include the parity drive, so we don't have to
  288. * change the math
  289. */
  290. factor = btrfs_bg_type_to_factor(profile);
  291. avail = div_u64(avail, factor);
  292. /*
  293. * If we aren't flushing all things, let us overcommit up to
  294. * 1/2th of the space. If we can flush, don't let us overcommit
  295. * too much, let it overcommit up to 1/8 of the space.
  296. */
  297. if (flush == BTRFS_RESERVE_FLUSH_ALL)
  298. avail >>= 3;
  299. else
  300. avail >>= 1;
  301. return avail;
  302. }
  303. int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
  304. struct btrfs_space_info *space_info, u64 bytes,
  305. enum btrfs_reserve_flush_enum flush)
  306. {
  307. u64 avail;
  308. u64 used;
  309. /* Don't overcommit when in mixed mode */
  310. if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
  311. return 0;
  312. used = btrfs_space_info_used(space_info, true);
  313. avail = calc_available_free_space(fs_info, space_info, flush);
  314. if (used + bytes < space_info->total_bytes + avail)
  315. return 1;
  316. return 0;
  317. }
  318. static void remove_ticket(struct btrfs_space_info *space_info,
  319. struct reserve_ticket *ticket)
  320. {
  321. if (!list_empty(&ticket->list)) {
  322. list_del_init(&ticket->list);
  323. ASSERT(space_info->reclaim_size >= ticket->bytes);
  324. space_info->reclaim_size -= ticket->bytes;
  325. }
  326. }
  327. /*
  328. * This is for space we already have accounted in space_info->bytes_may_use, so
  329. * basically when we're returning space from block_rsv's.
  330. */
  331. void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
  332. struct btrfs_space_info *space_info)
  333. {
  334. struct list_head *head;
  335. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
  336. lockdep_assert_held(&space_info->lock);
  337. head = &space_info->priority_tickets;
  338. again:
  339. while (!list_empty(head)) {
  340. struct reserve_ticket *ticket;
  341. u64 used = btrfs_space_info_used(space_info, true);
  342. ticket = list_first_entry(head, struct reserve_ticket, list);
  343. /* Check and see if our ticket can be satisified now. */
  344. if ((used + ticket->bytes <= space_info->total_bytes) ||
  345. btrfs_can_overcommit(fs_info, space_info, ticket->bytes,
  346. flush)) {
  347. btrfs_space_info_update_bytes_may_use(fs_info,
  348. space_info,
  349. ticket->bytes);
  350. remove_ticket(space_info, ticket);
  351. ticket->bytes = 0;
  352. space_info->tickets_id++;
  353. wake_up(&ticket->wait);
  354. } else {
  355. break;
  356. }
  357. }
  358. if (head == &space_info->priority_tickets) {
  359. head = &space_info->tickets;
  360. flush = BTRFS_RESERVE_FLUSH_ALL;
  361. goto again;
  362. }
  363. }
  364. #define DUMP_BLOCK_RSV(fs_info, rsv_name) \
  365. do { \
  366. struct btrfs_block_rsv *__rsv = &(fs_info)->rsv_name; \
  367. spin_lock(&__rsv->lock); \
  368. btrfs_info(fs_info, #rsv_name ": size %llu reserved %llu", \
  369. __rsv->size, __rsv->reserved); \
  370. spin_unlock(&__rsv->lock); \
  371. } while (0)
  372. static void __btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
  373. struct btrfs_space_info *info)
  374. {
  375. lockdep_assert_held(&info->lock);
  376. /* The free space could be negative in case of overcommit */
  377. btrfs_info(fs_info, "space_info %llu has %lld free, is %sfull",
  378. info->flags,
  379. (s64)(info->total_bytes - btrfs_space_info_used(info, true)),
  380. info->full ? "" : "not ");
  381. btrfs_info(fs_info,
  382. "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
  383. info->total_bytes, info->bytes_used, info->bytes_pinned,
  384. info->bytes_reserved, info->bytes_may_use,
  385. info->bytes_readonly);
  386. DUMP_BLOCK_RSV(fs_info, global_block_rsv);
  387. DUMP_BLOCK_RSV(fs_info, trans_block_rsv);
  388. DUMP_BLOCK_RSV(fs_info, chunk_block_rsv);
  389. DUMP_BLOCK_RSV(fs_info, delayed_block_rsv);
  390. DUMP_BLOCK_RSV(fs_info, delayed_refs_rsv);
  391. }
  392. void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
  393. struct btrfs_space_info *info, u64 bytes,
  394. int dump_block_groups)
  395. {
  396. struct btrfs_block_group *cache;
  397. int index = 0;
  398. spin_lock(&info->lock);
  399. __btrfs_dump_space_info(fs_info, info);
  400. spin_unlock(&info->lock);
  401. if (!dump_block_groups)
  402. return;
  403. down_read(&info->groups_sem);
  404. again:
  405. list_for_each_entry(cache, &info->block_groups[index], list) {
  406. spin_lock(&cache->lock);
  407. btrfs_info(fs_info,
  408. "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
  409. cache->start, cache->length, cache->used, cache->pinned,
  410. cache->reserved, cache->ro ? "[readonly]" : "");
  411. spin_unlock(&cache->lock);
  412. btrfs_dump_free_space(cache, bytes);
  413. }
  414. if (++index < BTRFS_NR_RAID_TYPES)
  415. goto again;
  416. up_read(&info->groups_sem);
  417. }
  418. static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
  419. u64 to_reclaim)
  420. {
  421. u64 bytes;
  422. u64 nr;
  423. bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
  424. nr = div64_u64(to_reclaim, bytes);
  425. if (!nr)
  426. nr = 1;
  427. return nr;
  428. }
  429. #define EXTENT_SIZE_PER_ITEM SZ_256K
  430. /*
  431. * shrink metadata reservation for delalloc
  432. */
  433. static void shrink_delalloc(struct btrfs_fs_info *fs_info,
  434. struct btrfs_space_info *space_info,
  435. u64 to_reclaim, bool wait_ordered)
  436. {
  437. struct btrfs_trans_handle *trans;
  438. u64 delalloc_bytes;
  439. u64 dio_bytes;
  440. u64 items;
  441. long time_left;
  442. int loops;
  443. /* Calc the number of the pages we need flush for space reservation */
  444. if (to_reclaim == U64_MAX) {
  445. items = U64_MAX;
  446. } else {
  447. /*
  448. * to_reclaim is set to however much metadata we need to
  449. * reclaim, but reclaiming that much data doesn't really track
  450. * exactly, so increase the amount to reclaim by 2x in order to
  451. * make sure we're flushing enough delalloc to hopefully reclaim
  452. * some metadata reservations.
  453. */
  454. items = calc_reclaim_items_nr(fs_info, to_reclaim) * 2;
  455. to_reclaim = items * EXTENT_SIZE_PER_ITEM;
  456. }
  457. trans = (struct btrfs_trans_handle *)current->journal_info;
  458. delalloc_bytes = percpu_counter_sum_positive(
  459. &fs_info->delalloc_bytes);
  460. dio_bytes = percpu_counter_sum_positive(&fs_info->dio_bytes);
  461. if (delalloc_bytes == 0 && dio_bytes == 0) {
  462. if (trans)
  463. return;
  464. if (wait_ordered)
  465. btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
  466. return;
  467. }
  468. /*
  469. * If we are doing more ordered than delalloc we need to just wait on
  470. * ordered extents, otherwise we'll waste time trying to flush delalloc
  471. * that likely won't give us the space back we need.
  472. */
  473. if (dio_bytes > delalloc_bytes)
  474. wait_ordered = true;
  475. loops = 0;
  476. while ((delalloc_bytes || dio_bytes) && loops < 3) {
  477. u64 nr_pages = min(delalloc_bytes, to_reclaim) >> PAGE_SHIFT;
  478. btrfs_start_delalloc_roots(fs_info, nr_pages, true);
  479. loops++;
  480. if (wait_ordered && !trans) {
  481. btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
  482. } else {
  483. time_left = schedule_timeout_killable(1);
  484. if (time_left)
  485. break;
  486. }
  487. spin_lock(&space_info->lock);
  488. if (list_empty(&space_info->tickets) &&
  489. list_empty(&space_info->priority_tickets)) {
  490. spin_unlock(&space_info->lock);
  491. break;
  492. }
  493. spin_unlock(&space_info->lock);
  494. delalloc_bytes = percpu_counter_sum_positive(
  495. &fs_info->delalloc_bytes);
  496. dio_bytes = percpu_counter_sum_positive(&fs_info->dio_bytes);
  497. }
  498. }
  499. /**
  500. * maybe_commit_transaction - possibly commit the transaction if its ok to
  501. * @root - the root we're allocating for
  502. * @bytes - the number of bytes we want to reserve
  503. * @force - force the commit
  504. *
  505. * This will check to make sure that committing the transaction will actually
  506. * get us somewhere and then commit the transaction if it does. Otherwise it
  507. * will return -ENOSPC.
  508. */
  509. static int may_commit_transaction(struct btrfs_fs_info *fs_info,
  510. struct btrfs_space_info *space_info)
  511. {
  512. struct reserve_ticket *ticket = NULL;
  513. struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
  514. struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
  515. struct btrfs_block_rsv *trans_rsv = &fs_info->trans_block_rsv;
  516. struct btrfs_trans_handle *trans;
  517. u64 reclaim_bytes = 0;
  518. u64 bytes_needed = 0;
  519. u64 cur_free_bytes = 0;
  520. trans = (struct btrfs_trans_handle *)current->journal_info;
  521. if (trans)
  522. return -EAGAIN;
  523. spin_lock(&space_info->lock);
  524. cur_free_bytes = btrfs_space_info_used(space_info, true);
  525. if (cur_free_bytes < space_info->total_bytes)
  526. cur_free_bytes = space_info->total_bytes - cur_free_bytes;
  527. else
  528. cur_free_bytes = 0;
  529. if (!list_empty(&space_info->priority_tickets))
  530. ticket = list_first_entry(&space_info->priority_tickets,
  531. struct reserve_ticket, list);
  532. else if (!list_empty(&space_info->tickets))
  533. ticket = list_first_entry(&space_info->tickets,
  534. struct reserve_ticket, list);
  535. if (ticket)
  536. bytes_needed = ticket->bytes;
  537. if (bytes_needed > cur_free_bytes)
  538. bytes_needed -= cur_free_bytes;
  539. else
  540. bytes_needed = 0;
  541. spin_unlock(&space_info->lock);
  542. if (!bytes_needed)
  543. return 0;
  544. trans = btrfs_join_transaction(fs_info->extent_root);
  545. if (IS_ERR(trans))
  546. return PTR_ERR(trans);
  547. /*
  548. * See if there is enough pinned space to make this reservation, or if
  549. * we have block groups that are going to be freed, allowing us to
  550. * possibly do a chunk allocation the next loop through.
  551. */
  552. if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags) ||
  553. __percpu_counter_compare(&space_info->total_bytes_pinned,
  554. bytes_needed,
  555. BTRFS_TOTAL_BYTES_PINNED_BATCH) >= 0)
  556. goto commit;
  557. /*
  558. * See if there is some space in the delayed insertion reserve for this
  559. * reservation. If the space_info's don't match (like for DATA or
  560. * SYSTEM) then just go enospc, reclaiming this space won't recover any
  561. * space to satisfy those reservations.
  562. */
  563. if (space_info != delayed_rsv->space_info)
  564. goto enospc;
  565. spin_lock(&delayed_rsv->lock);
  566. reclaim_bytes += delayed_rsv->reserved;
  567. spin_unlock(&delayed_rsv->lock);
  568. spin_lock(&delayed_refs_rsv->lock);
  569. reclaim_bytes += delayed_refs_rsv->reserved;
  570. spin_unlock(&delayed_refs_rsv->lock);
  571. spin_lock(&trans_rsv->lock);
  572. reclaim_bytes += trans_rsv->reserved;
  573. spin_unlock(&trans_rsv->lock);
  574. if (reclaim_bytes >= bytes_needed)
  575. goto commit;
  576. bytes_needed -= reclaim_bytes;
  577. if (__percpu_counter_compare(&space_info->total_bytes_pinned,
  578. bytes_needed,
  579. BTRFS_TOTAL_BYTES_PINNED_BATCH) < 0)
  580. goto enospc;
  581. commit:
  582. return btrfs_commit_transaction(trans);
  583. enospc:
  584. btrfs_end_transaction(trans);
  585. return -ENOSPC;
  586. }
  587. /*
  588. * Try to flush some data based on policy set by @state. This is only advisory
  589. * and may fail for various reasons. The caller is supposed to examine the
  590. * state of @space_info to detect the outcome.
  591. */
  592. static void flush_space(struct btrfs_fs_info *fs_info,
  593. struct btrfs_space_info *space_info, u64 num_bytes,
  594. int state)
  595. {
  596. struct btrfs_root *root = fs_info->extent_root;
  597. struct btrfs_trans_handle *trans;
  598. int nr;
  599. int ret = 0;
  600. switch (state) {
  601. case FLUSH_DELAYED_ITEMS_NR:
  602. case FLUSH_DELAYED_ITEMS:
  603. if (state == FLUSH_DELAYED_ITEMS_NR)
  604. nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
  605. else
  606. nr = -1;
  607. trans = btrfs_join_transaction(root);
  608. if (IS_ERR(trans)) {
  609. ret = PTR_ERR(trans);
  610. break;
  611. }
  612. ret = btrfs_run_delayed_items_nr(trans, nr);
  613. btrfs_end_transaction(trans);
  614. break;
  615. case FLUSH_DELALLOC:
  616. case FLUSH_DELALLOC_WAIT:
  617. shrink_delalloc(fs_info, space_info, num_bytes,
  618. state == FLUSH_DELALLOC_WAIT);
  619. break;
  620. case FLUSH_DELAYED_REFS_NR:
  621. case FLUSH_DELAYED_REFS:
  622. trans = btrfs_join_transaction(root);
  623. if (IS_ERR(trans)) {
  624. ret = PTR_ERR(trans);
  625. break;
  626. }
  627. if (state == FLUSH_DELAYED_REFS_NR)
  628. nr = calc_reclaim_items_nr(fs_info, num_bytes);
  629. else
  630. nr = 0;
  631. btrfs_run_delayed_refs(trans, nr);
  632. btrfs_end_transaction(trans);
  633. break;
  634. case ALLOC_CHUNK:
  635. case ALLOC_CHUNK_FORCE:
  636. trans = btrfs_join_transaction(root);
  637. if (IS_ERR(trans)) {
  638. ret = PTR_ERR(trans);
  639. break;
  640. }
  641. ret = btrfs_chunk_alloc(trans,
  642. btrfs_get_alloc_profile(fs_info, space_info->flags),
  643. (state == ALLOC_CHUNK) ? CHUNK_ALLOC_NO_FORCE :
  644. CHUNK_ALLOC_FORCE);
  645. btrfs_end_transaction(trans);
  646. if (ret > 0 || ret == -ENOSPC)
  647. ret = 0;
  648. break;
  649. case RUN_DELAYED_IPUTS:
  650. /*
  651. * If we have pending delayed iputs then we could free up a
  652. * bunch of pinned space, so make sure we run the iputs before
  653. * we do our pinned bytes check below.
  654. */
  655. btrfs_run_delayed_iputs(fs_info);
  656. btrfs_wait_on_delayed_iputs(fs_info);
  657. break;
  658. case COMMIT_TRANS:
  659. ret = may_commit_transaction(fs_info, space_info);
  660. break;
  661. default:
  662. ret = -ENOSPC;
  663. break;
  664. }
  665. trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
  666. ret);
  667. return;
  668. }
  669. static inline u64
  670. btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
  671. struct btrfs_space_info *space_info)
  672. {
  673. u64 used;
  674. u64 avail;
  675. u64 expected;
  676. u64 to_reclaim = space_info->reclaim_size;
  677. lockdep_assert_held(&space_info->lock);
  678. avail = calc_available_free_space(fs_info, space_info,
  679. BTRFS_RESERVE_FLUSH_ALL);
  680. used = btrfs_space_info_used(space_info, true);
  681. /*
  682. * We may be flushing because suddenly we have less space than we had
  683. * before, and now we're well over-committed based on our current free
  684. * space. If that's the case add in our overage so we make sure to put
  685. * appropriate pressure on the flushing state machine.
  686. */
  687. if (space_info->total_bytes + avail < used)
  688. to_reclaim += used - (space_info->total_bytes + avail);
  689. if (to_reclaim)
  690. return to_reclaim;
  691. to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
  692. if (btrfs_can_overcommit(fs_info, space_info, to_reclaim,
  693. BTRFS_RESERVE_FLUSH_ALL))
  694. return 0;
  695. used = btrfs_space_info_used(space_info, true);
  696. if (btrfs_can_overcommit(fs_info, space_info, SZ_1M,
  697. BTRFS_RESERVE_FLUSH_ALL))
  698. expected = div_factor_fine(space_info->total_bytes, 95);
  699. else
  700. expected = div_factor_fine(space_info->total_bytes, 90);
  701. if (used > expected)
  702. to_reclaim = used - expected;
  703. else
  704. to_reclaim = 0;
  705. to_reclaim = min(to_reclaim, space_info->bytes_may_use +
  706. space_info->bytes_reserved);
  707. return to_reclaim;
  708. }
  709. static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info,
  710. struct btrfs_space_info *space_info,
  711. u64 used)
  712. {
  713. u64 thresh = div_factor_fine(space_info->total_bytes, 98);
  714. /* If we're just plain full then async reclaim just slows us down. */
  715. if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
  716. return 0;
  717. if (!btrfs_calc_reclaim_metadata_size(fs_info, space_info))
  718. return 0;
  719. return (used >= thresh && !btrfs_fs_closing(fs_info) &&
  720. !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
  721. }
  722. static bool steal_from_global_rsv(struct btrfs_fs_info *fs_info,
  723. struct btrfs_space_info *space_info,
  724. struct reserve_ticket *ticket)
  725. {
  726. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  727. u64 min_bytes;
  728. if (global_rsv->space_info != space_info)
  729. return false;
  730. spin_lock(&global_rsv->lock);
  731. min_bytes = div_factor(global_rsv->size, 1);
  732. if (global_rsv->reserved < min_bytes + ticket->bytes) {
  733. spin_unlock(&global_rsv->lock);
  734. return false;
  735. }
  736. global_rsv->reserved -= ticket->bytes;
  737. remove_ticket(space_info, ticket);
  738. ticket->bytes = 0;
  739. wake_up(&ticket->wait);
  740. space_info->tickets_id++;
  741. if (global_rsv->reserved < global_rsv->size)
  742. global_rsv->full = 0;
  743. spin_unlock(&global_rsv->lock);
  744. return true;
  745. }
  746. /*
  747. * maybe_fail_all_tickets - we've exhausted our flushing, start failing tickets
  748. * @fs_info - fs_info for this fs
  749. * @space_info - the space info we were flushing
  750. *
  751. * We call this when we've exhausted our flushing ability and haven't made
  752. * progress in satisfying tickets. The reservation code handles tickets in
  753. * order, so if there is a large ticket first and then smaller ones we could
  754. * very well satisfy the smaller tickets. This will attempt to wake up any
  755. * tickets in the list to catch this case.
  756. *
  757. * This function returns true if it was able to make progress by clearing out
  758. * other tickets, or if it stumbles across a ticket that was smaller than the
  759. * first ticket.
  760. */
  761. static bool maybe_fail_all_tickets(struct btrfs_fs_info *fs_info,
  762. struct btrfs_space_info *space_info)
  763. {
  764. struct reserve_ticket *ticket;
  765. u64 tickets_id = space_info->tickets_id;
  766. u64 first_ticket_bytes = 0;
  767. if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
  768. btrfs_info(fs_info, "cannot satisfy tickets, dumping space info");
  769. __btrfs_dump_space_info(fs_info, space_info);
  770. }
  771. while (!list_empty(&space_info->tickets) &&
  772. tickets_id == space_info->tickets_id) {
  773. ticket = list_first_entry(&space_info->tickets,
  774. struct reserve_ticket, list);
  775. if (ticket->steal &&
  776. steal_from_global_rsv(fs_info, space_info, ticket))
  777. return true;
  778. /*
  779. * may_commit_transaction will avoid committing the transaction
  780. * if it doesn't feel like the space reclaimed by the commit
  781. * would result in the ticket succeeding. However if we have a
  782. * smaller ticket in the queue it may be small enough to be
  783. * satisified by committing the transaction, so if any
  784. * subsequent ticket is smaller than the first ticket go ahead
  785. * and send us back for another loop through the enospc flushing
  786. * code.
  787. */
  788. if (first_ticket_bytes == 0)
  789. first_ticket_bytes = ticket->bytes;
  790. else if (first_ticket_bytes > ticket->bytes)
  791. return true;
  792. if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
  793. btrfs_info(fs_info, "failing ticket with %llu bytes",
  794. ticket->bytes);
  795. remove_ticket(space_info, ticket);
  796. ticket->error = -ENOSPC;
  797. wake_up(&ticket->wait);
  798. /*
  799. * We're just throwing tickets away, so more flushing may not
  800. * trip over btrfs_try_granting_tickets, so we need to call it
  801. * here to see if we can make progress with the next ticket in
  802. * the list.
  803. */
  804. btrfs_try_granting_tickets(fs_info, space_info);
  805. }
  806. return (tickets_id != space_info->tickets_id);
  807. }
  808. /*
  809. * This is for normal flushers, we can wait all goddamned day if we want to. We
  810. * will loop and continuously try to flush as long as we are making progress.
  811. * We count progress as clearing off tickets each time we have to loop.
  812. */
  813. static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
  814. {
  815. struct btrfs_fs_info *fs_info;
  816. struct btrfs_space_info *space_info;
  817. u64 to_reclaim;
  818. int flush_state;
  819. int commit_cycles = 0;
  820. u64 last_tickets_id;
  821. fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
  822. space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  823. spin_lock(&space_info->lock);
  824. to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info);
  825. if (!to_reclaim) {
  826. space_info->flush = 0;
  827. spin_unlock(&space_info->lock);
  828. return;
  829. }
  830. last_tickets_id = space_info->tickets_id;
  831. spin_unlock(&space_info->lock);
  832. flush_state = FLUSH_DELAYED_ITEMS_NR;
  833. do {
  834. flush_space(fs_info, space_info, to_reclaim, flush_state);
  835. spin_lock(&space_info->lock);
  836. if (list_empty(&space_info->tickets)) {
  837. space_info->flush = 0;
  838. spin_unlock(&space_info->lock);
  839. return;
  840. }
  841. to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
  842. space_info);
  843. if (last_tickets_id == space_info->tickets_id) {
  844. flush_state++;
  845. } else {
  846. last_tickets_id = space_info->tickets_id;
  847. flush_state = FLUSH_DELAYED_ITEMS_NR;
  848. if (commit_cycles)
  849. commit_cycles--;
  850. }
  851. /*
  852. * We don't want to force a chunk allocation until we've tried
  853. * pretty hard to reclaim space. Think of the case where we
  854. * freed up a bunch of space and so have a lot of pinned space
  855. * to reclaim. We would rather use that than possibly create a
  856. * underutilized metadata chunk. So if this is our first run
  857. * through the flushing state machine skip ALLOC_CHUNK_FORCE and
  858. * commit the transaction. If nothing has changed the next go
  859. * around then we can force a chunk allocation.
  860. */
  861. if (flush_state == ALLOC_CHUNK_FORCE && !commit_cycles)
  862. flush_state++;
  863. if (flush_state > COMMIT_TRANS) {
  864. commit_cycles++;
  865. if (commit_cycles > 2) {
  866. if (maybe_fail_all_tickets(fs_info, space_info)) {
  867. flush_state = FLUSH_DELAYED_ITEMS_NR;
  868. commit_cycles--;
  869. } else {
  870. space_info->flush = 0;
  871. }
  872. } else {
  873. flush_state = FLUSH_DELAYED_ITEMS_NR;
  874. }
  875. }
  876. spin_unlock(&space_info->lock);
  877. } while (flush_state <= COMMIT_TRANS);
  878. }
  879. /*
  880. * FLUSH_DELALLOC_WAIT:
  881. * Space is freed from flushing delalloc in one of two ways.
  882. *
  883. * 1) compression is on and we allocate less space than we reserved
  884. * 2) we are overwriting existing space
  885. *
  886. * For #1 that extra space is reclaimed as soon as the delalloc pages are
  887. * COWed, by way of btrfs_add_reserved_bytes() which adds the actual extent
  888. * length to ->bytes_reserved, and subtracts the reserved space from
  889. * ->bytes_may_use.
  890. *
  891. * For #2 this is trickier. Once the ordered extent runs we will drop the
  892. * extent in the range we are overwriting, which creates a delayed ref for
  893. * that freed extent. This however is not reclaimed until the transaction
  894. * commits, thus the next stages.
  895. *
  896. * RUN_DELAYED_IPUTS
  897. * If we are freeing inodes, we want to make sure all delayed iputs have
  898. * completed, because they could have been on an inode with i_nlink == 0, and
  899. * thus have been truncated and freed up space. But again this space is not
  900. * immediately re-usable, it comes in the form of a delayed ref, which must be
  901. * run and then the transaction must be committed.
  902. *
  903. * FLUSH_DELAYED_REFS
  904. * The above two cases generate delayed refs that will affect
  905. * ->total_bytes_pinned. However this counter can be inconsistent with
  906. * reality if there are outstanding delayed refs. This is because we adjust
  907. * the counter based solely on the current set of delayed refs and disregard
  908. * any on-disk state which might include more refs. So for example, if we
  909. * have an extent with 2 references, but we only drop 1, we'll see that there
  910. * is a negative delayed ref count for the extent and assume that the space
  911. * will be freed, and thus increase ->total_bytes_pinned.
  912. *
  913. * Running the delayed refs gives us the actual real view of what will be
  914. * freed at the transaction commit time. This stage will not actually free
  915. * space for us, it just makes sure that may_commit_transaction() has all of
  916. * the information it needs to make the right decision.
  917. *
  918. * COMMIT_TRANS
  919. * This is where we reclaim all of the pinned space generated by the previous
  920. * two stages. We will not commit the transaction if we don't think we're
  921. * likely to satisfy our request, which means if our current free space +
  922. * total_bytes_pinned < reservation we will not commit. This is why the
  923. * previous states are actually important, to make sure we know for sure
  924. * whether committing the transaction will allow us to make progress.
  925. *
  926. * ALLOC_CHUNK_FORCE
  927. * For data we start with alloc chunk force, however we could have been full
  928. * before, and then the transaction commit could have freed new block groups,
  929. * so if we now have space to allocate do the force chunk allocation.
  930. */
  931. static const enum btrfs_flush_state data_flush_states[] = {
  932. FLUSH_DELALLOC_WAIT,
  933. RUN_DELAYED_IPUTS,
  934. FLUSH_DELAYED_REFS,
  935. COMMIT_TRANS,
  936. ALLOC_CHUNK_FORCE,
  937. };
  938. static void btrfs_async_reclaim_data_space(struct work_struct *work)
  939. {
  940. struct btrfs_fs_info *fs_info;
  941. struct btrfs_space_info *space_info;
  942. u64 last_tickets_id;
  943. int flush_state = 0;
  944. fs_info = container_of(work, struct btrfs_fs_info, async_data_reclaim_work);
  945. space_info = fs_info->data_sinfo;
  946. spin_lock(&space_info->lock);
  947. if (list_empty(&space_info->tickets)) {
  948. space_info->flush = 0;
  949. spin_unlock(&space_info->lock);
  950. return;
  951. }
  952. last_tickets_id = space_info->tickets_id;
  953. spin_unlock(&space_info->lock);
  954. while (!space_info->full) {
  955. flush_space(fs_info, space_info, U64_MAX, ALLOC_CHUNK_FORCE);
  956. spin_lock(&space_info->lock);
  957. if (list_empty(&space_info->tickets)) {
  958. space_info->flush = 0;
  959. spin_unlock(&space_info->lock);
  960. return;
  961. }
  962. last_tickets_id = space_info->tickets_id;
  963. spin_unlock(&space_info->lock);
  964. }
  965. while (flush_state < ARRAY_SIZE(data_flush_states)) {
  966. flush_space(fs_info, space_info, U64_MAX,
  967. data_flush_states[flush_state]);
  968. spin_lock(&space_info->lock);
  969. if (list_empty(&space_info->tickets)) {
  970. space_info->flush = 0;
  971. spin_unlock(&space_info->lock);
  972. return;
  973. }
  974. if (last_tickets_id == space_info->tickets_id) {
  975. flush_state++;
  976. } else {
  977. last_tickets_id = space_info->tickets_id;
  978. flush_state = 0;
  979. }
  980. if (flush_state >= ARRAY_SIZE(data_flush_states)) {
  981. if (space_info->full) {
  982. if (maybe_fail_all_tickets(fs_info, space_info))
  983. flush_state = 0;
  984. else
  985. space_info->flush = 0;
  986. } else {
  987. flush_state = 0;
  988. }
  989. }
  990. spin_unlock(&space_info->lock);
  991. }
  992. }
  993. void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info)
  994. {
  995. INIT_WORK(&fs_info->async_reclaim_work, btrfs_async_reclaim_metadata_space);
  996. INIT_WORK(&fs_info->async_data_reclaim_work, btrfs_async_reclaim_data_space);
  997. }
  998. static const enum btrfs_flush_state priority_flush_states[] = {
  999. FLUSH_DELAYED_ITEMS_NR,
  1000. FLUSH_DELAYED_ITEMS,
  1001. ALLOC_CHUNK,
  1002. };
  1003. static const enum btrfs_flush_state evict_flush_states[] = {
  1004. FLUSH_DELAYED_ITEMS_NR,
  1005. FLUSH_DELAYED_ITEMS,
  1006. FLUSH_DELAYED_REFS_NR,
  1007. FLUSH_DELAYED_REFS,
  1008. FLUSH_DELALLOC,
  1009. FLUSH_DELALLOC_WAIT,
  1010. ALLOC_CHUNK,
  1011. COMMIT_TRANS,
  1012. };
  1013. static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
  1014. struct btrfs_space_info *space_info,
  1015. struct reserve_ticket *ticket,
  1016. const enum btrfs_flush_state *states,
  1017. int states_nr)
  1018. {
  1019. u64 to_reclaim;
  1020. int flush_state;
  1021. spin_lock(&space_info->lock);
  1022. to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info);
  1023. if (!to_reclaim) {
  1024. spin_unlock(&space_info->lock);
  1025. return;
  1026. }
  1027. spin_unlock(&space_info->lock);
  1028. flush_state = 0;
  1029. do {
  1030. flush_space(fs_info, space_info, to_reclaim, states[flush_state]);
  1031. flush_state++;
  1032. spin_lock(&space_info->lock);
  1033. if (ticket->bytes == 0) {
  1034. spin_unlock(&space_info->lock);
  1035. return;
  1036. }
  1037. spin_unlock(&space_info->lock);
  1038. } while (flush_state < states_nr);
  1039. }
  1040. static void priority_reclaim_data_space(struct btrfs_fs_info *fs_info,
  1041. struct btrfs_space_info *space_info,
  1042. struct reserve_ticket *ticket)
  1043. {
  1044. while (!space_info->full) {
  1045. flush_space(fs_info, space_info, U64_MAX, ALLOC_CHUNK_FORCE);
  1046. spin_lock(&space_info->lock);
  1047. if (ticket->bytes == 0) {
  1048. spin_unlock(&space_info->lock);
  1049. return;
  1050. }
  1051. spin_unlock(&space_info->lock);
  1052. }
  1053. }
  1054. static void wait_reserve_ticket(struct btrfs_fs_info *fs_info,
  1055. struct btrfs_space_info *space_info,
  1056. struct reserve_ticket *ticket)
  1057. {
  1058. DEFINE_WAIT(wait);
  1059. int ret = 0;
  1060. spin_lock(&space_info->lock);
  1061. while (ticket->bytes > 0 && ticket->error == 0) {
  1062. ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
  1063. if (ret) {
  1064. /*
  1065. * Delete us from the list. After we unlock the space
  1066. * info, we don't want the async reclaim job to reserve
  1067. * space for this ticket. If that would happen, then the
  1068. * ticket's task would not known that space was reserved
  1069. * despite getting an error, resulting in a space leak
  1070. * (bytes_may_use counter of our space_info).
  1071. */
  1072. remove_ticket(space_info, ticket);
  1073. ticket->error = -EINTR;
  1074. break;
  1075. }
  1076. spin_unlock(&space_info->lock);
  1077. schedule();
  1078. finish_wait(&ticket->wait, &wait);
  1079. spin_lock(&space_info->lock);
  1080. }
  1081. spin_unlock(&space_info->lock);
  1082. }
  1083. /**
  1084. * handle_reserve_ticket - do the appropriate flushing and waiting for a ticket
  1085. * @fs_info - the fs
  1086. * @space_info - the space_info for the reservation
  1087. * @ticket - the ticket for the reservation
  1088. * @flush - how much we can flush
  1089. *
  1090. * This does the work of figuring out how to flush for the ticket, waiting for
  1091. * the reservation, and returning the appropriate error if there is one.
  1092. */
  1093. static int handle_reserve_ticket(struct btrfs_fs_info *fs_info,
  1094. struct btrfs_space_info *space_info,
  1095. struct reserve_ticket *ticket,
  1096. enum btrfs_reserve_flush_enum flush)
  1097. {
  1098. int ret;
  1099. switch (flush) {
  1100. case BTRFS_RESERVE_FLUSH_DATA:
  1101. case BTRFS_RESERVE_FLUSH_ALL:
  1102. case BTRFS_RESERVE_FLUSH_ALL_STEAL:
  1103. wait_reserve_ticket(fs_info, space_info, ticket);
  1104. break;
  1105. case BTRFS_RESERVE_FLUSH_LIMIT:
  1106. priority_reclaim_metadata_space(fs_info, space_info, ticket,
  1107. priority_flush_states,
  1108. ARRAY_SIZE(priority_flush_states));
  1109. break;
  1110. case BTRFS_RESERVE_FLUSH_EVICT:
  1111. priority_reclaim_metadata_space(fs_info, space_info, ticket,
  1112. evict_flush_states,
  1113. ARRAY_SIZE(evict_flush_states));
  1114. break;
  1115. case BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE:
  1116. priority_reclaim_data_space(fs_info, space_info, ticket);
  1117. break;
  1118. default:
  1119. ASSERT(0);
  1120. break;
  1121. }
  1122. spin_lock(&space_info->lock);
  1123. ret = ticket->error;
  1124. if (ticket->bytes || ticket->error) {
  1125. /*
  1126. * We were a priority ticket, so we need to delete ourselves
  1127. * from the list. Because we could have other priority tickets
  1128. * behind us that require less space, run
  1129. * btrfs_try_granting_tickets() to see if their reservations can
  1130. * now be made.
  1131. */
  1132. if (!list_empty(&ticket->list)) {
  1133. remove_ticket(space_info, ticket);
  1134. btrfs_try_granting_tickets(fs_info, space_info);
  1135. }
  1136. if (!ret)
  1137. ret = -ENOSPC;
  1138. }
  1139. spin_unlock(&space_info->lock);
  1140. ASSERT(list_empty(&ticket->list));
  1141. /*
  1142. * Check that we can't have an error set if the reservation succeeded,
  1143. * as that would confuse tasks and lead them to error out without
  1144. * releasing reserved space (if an error happens the expectation is that
  1145. * space wasn't reserved at all).
  1146. */
  1147. ASSERT(!(ticket->bytes == 0 && ticket->error));
  1148. return ret;
  1149. }
  1150. /*
  1151. * This returns true if this flush state will go through the ordinary flushing
  1152. * code.
  1153. */
  1154. static inline bool is_normal_flushing(enum btrfs_reserve_flush_enum flush)
  1155. {
  1156. return (flush == BTRFS_RESERVE_FLUSH_ALL) ||
  1157. (flush == BTRFS_RESERVE_FLUSH_ALL_STEAL);
  1158. }
  1159. /**
  1160. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  1161. * @root - the root we're allocating for
  1162. * @space_info - the space info we want to allocate from
  1163. * @orig_bytes - the number of bytes we want
  1164. * @flush - whether or not we can flush to make our reservation
  1165. *
  1166. * This will reserve orig_bytes number of bytes from the space info associated
  1167. * with the block_rsv. If there is not enough space it will make an attempt to
  1168. * flush out space to make room. It will do this by flushing delalloc if
  1169. * possible or committing the transaction. If flush is 0 then no attempts to
  1170. * regain reservations will be made and this will fail if there is not enough
  1171. * space already.
  1172. */
  1173. static int __reserve_bytes(struct btrfs_fs_info *fs_info,
  1174. struct btrfs_space_info *space_info, u64 orig_bytes,
  1175. enum btrfs_reserve_flush_enum flush)
  1176. {
  1177. struct work_struct *async_work;
  1178. struct reserve_ticket ticket;
  1179. u64 used;
  1180. int ret = 0;
  1181. bool pending_tickets;
  1182. ASSERT(orig_bytes);
  1183. ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
  1184. if (flush == BTRFS_RESERVE_FLUSH_DATA)
  1185. async_work = &fs_info->async_data_reclaim_work;
  1186. else
  1187. async_work = &fs_info->async_reclaim_work;
  1188. spin_lock(&space_info->lock);
  1189. ret = -ENOSPC;
  1190. used = btrfs_space_info_used(space_info, true);
  1191. /*
  1192. * We don't want NO_FLUSH allocations to jump everybody, they can
  1193. * generally handle ENOSPC in a different way, so treat them the same as
  1194. * normal flushers when it comes to skipping pending tickets.
  1195. */
  1196. if (is_normal_flushing(flush) || (flush == BTRFS_RESERVE_NO_FLUSH))
  1197. pending_tickets = !list_empty(&space_info->tickets) ||
  1198. !list_empty(&space_info->priority_tickets);
  1199. else
  1200. pending_tickets = !list_empty(&space_info->priority_tickets);
  1201. /*
  1202. * Carry on if we have enough space (short-circuit) OR call
  1203. * can_overcommit() to ensure we can overcommit to continue.
  1204. */
  1205. if (!pending_tickets &&
  1206. ((used + orig_bytes <= space_info->total_bytes) ||
  1207. btrfs_can_overcommit(fs_info, space_info, orig_bytes, flush))) {
  1208. btrfs_space_info_update_bytes_may_use(fs_info, space_info,
  1209. orig_bytes);
  1210. ret = 0;
  1211. }
  1212. /*
  1213. * If we couldn't make a reservation then setup our reservation ticket
  1214. * and kick the async worker if it's not already running.
  1215. *
  1216. * If we are a priority flusher then we just need to add our ticket to
  1217. * the list and we will do our own flushing further down.
  1218. */
  1219. if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
  1220. ticket.bytes = orig_bytes;
  1221. ticket.error = 0;
  1222. space_info->reclaim_size += ticket.bytes;
  1223. init_waitqueue_head(&ticket.wait);
  1224. ticket.steal = (flush == BTRFS_RESERVE_FLUSH_ALL_STEAL);
  1225. if (flush == BTRFS_RESERVE_FLUSH_ALL ||
  1226. flush == BTRFS_RESERVE_FLUSH_ALL_STEAL ||
  1227. flush == BTRFS_RESERVE_FLUSH_DATA) {
  1228. list_add_tail(&ticket.list, &space_info->tickets);
  1229. if (!space_info->flush) {
  1230. space_info->flush = 1;
  1231. trace_btrfs_trigger_flush(fs_info,
  1232. space_info->flags,
  1233. orig_bytes, flush,
  1234. "enospc");
  1235. queue_work(system_unbound_wq, async_work);
  1236. }
  1237. } else {
  1238. list_add_tail(&ticket.list,
  1239. &space_info->priority_tickets);
  1240. }
  1241. } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
  1242. used += orig_bytes;
  1243. /*
  1244. * We will do the space reservation dance during log replay,
  1245. * which means we won't have fs_info->fs_root set, so don't do
  1246. * the async reclaim as we will panic.
  1247. */
  1248. if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
  1249. need_do_async_reclaim(fs_info, space_info, used) &&
  1250. !work_busy(&fs_info->async_reclaim_work)) {
  1251. trace_btrfs_trigger_flush(fs_info, space_info->flags,
  1252. orig_bytes, flush, "preempt");
  1253. queue_work(system_unbound_wq,
  1254. &fs_info->async_reclaim_work);
  1255. }
  1256. }
  1257. spin_unlock(&space_info->lock);
  1258. if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
  1259. return ret;
  1260. return handle_reserve_ticket(fs_info, space_info, &ticket, flush);
  1261. }
  1262. /**
  1263. * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
  1264. * @root - the root we're allocating for
  1265. * @block_rsv - the block_rsv we're allocating for
  1266. * @orig_bytes - the number of bytes we want
  1267. * @flush - whether or not we can flush to make our reservation
  1268. *
  1269. * This will reserve orig_bytes number of bytes from the space info associated
  1270. * with the block_rsv. If there is not enough space it will make an attempt to
  1271. * flush out space to make room. It will do this by flushing delalloc if
  1272. * possible or committing the transaction. If flush is 0 then no attempts to
  1273. * regain reservations will be made and this will fail if there is not enough
  1274. * space already.
  1275. */
  1276. int btrfs_reserve_metadata_bytes(struct btrfs_root *root,
  1277. struct btrfs_block_rsv *block_rsv,
  1278. u64 orig_bytes,
  1279. enum btrfs_reserve_flush_enum flush)
  1280. {
  1281. struct btrfs_fs_info *fs_info = root->fs_info;
  1282. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  1283. int ret;
  1284. ret = __reserve_bytes(fs_info, block_rsv->space_info, orig_bytes, flush);
  1285. if (ret == -ENOSPC &&
  1286. unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
  1287. if (block_rsv != global_rsv &&
  1288. !btrfs_block_rsv_use_bytes(global_rsv, orig_bytes))
  1289. ret = 0;
  1290. }
  1291. if (ret == -ENOSPC) {
  1292. trace_btrfs_space_reservation(fs_info, "space_info:enospc",
  1293. block_rsv->space_info->flags,
  1294. orig_bytes, 1);
  1295. if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
  1296. btrfs_dump_space_info(fs_info, block_rsv->space_info,
  1297. orig_bytes, 0);
  1298. }
  1299. return ret;
  1300. }
  1301. /**
  1302. * btrfs_reserve_data_bytes - try to reserve data bytes for an allocation
  1303. * @fs_info - the filesystem
  1304. * @bytes - the number of bytes we need
  1305. * @flush - how we are allowed to flush
  1306. *
  1307. * This will reserve bytes from the data space info. If there is not enough
  1308. * space then we will attempt to flush space as specified by flush.
  1309. */
  1310. int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
  1311. enum btrfs_reserve_flush_enum flush)
  1312. {
  1313. struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
  1314. int ret;
  1315. ASSERT(flush == BTRFS_RESERVE_FLUSH_DATA ||
  1316. flush == BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE);
  1317. ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_DATA);
  1318. ret = __reserve_bytes(fs_info, data_sinfo, bytes, flush);
  1319. if (ret == -ENOSPC) {
  1320. trace_btrfs_space_reservation(fs_info, "space_info:enospc",
  1321. data_sinfo->flags, bytes, 1);
  1322. if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
  1323. btrfs_dump_space_info(fs_info, data_sinfo, bytes, 0);
  1324. }
  1325. return ret;
  1326. }