xfs_buf.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include <linux/stddef.h>
  8. #include <linux/errno.h>
  9. #include <linux/gfp.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/init.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/bio.h>
  14. #include <linux/sysctl.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/percpu.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/hash.h>
  20. #include <linux/kthread.h>
  21. #include <linux/migrate.h>
  22. #include <linux/backing-dev.h>
  23. #include <linux/freezer.h>
  24. #include "xfs_format.h"
  25. #include "xfs_log_format.h"
  26. #include "xfs_trans_resv.h"
  27. #include "xfs_sb.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_trace.h"
  30. #include "xfs_log.h"
  31. #include "xfs_errortag.h"
  32. #include "xfs_error.h"
  33. static kmem_zone_t *xfs_buf_zone;
  34. #define xb_to_gfp(flags) \
  35. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
  36. /*
  37. * Locking orders
  38. *
  39. * xfs_buf_ioacct_inc:
  40. * xfs_buf_ioacct_dec:
  41. * b_sema (caller holds)
  42. * b_lock
  43. *
  44. * xfs_buf_stale:
  45. * b_sema (caller holds)
  46. * b_lock
  47. * lru_lock
  48. *
  49. * xfs_buf_rele:
  50. * b_lock
  51. * pag_buf_lock
  52. * lru_lock
  53. *
  54. * xfs_buftarg_wait_rele
  55. * lru_lock
  56. * b_lock (trylock due to inversion)
  57. *
  58. * xfs_buftarg_isolate
  59. * lru_lock
  60. * b_lock (trylock due to inversion)
  61. */
  62. static inline int
  63. xfs_buf_is_vmapped(
  64. struct xfs_buf *bp)
  65. {
  66. /*
  67. * Return true if the buffer is vmapped.
  68. *
  69. * b_addr is null if the buffer is not mapped, but the code is clever
  70. * enough to know it doesn't have to map a single page, so the check has
  71. * to be both for b_addr and bp->b_page_count > 1.
  72. */
  73. return bp->b_addr && bp->b_page_count > 1;
  74. }
  75. static inline int
  76. xfs_buf_vmap_len(
  77. struct xfs_buf *bp)
  78. {
  79. return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
  80. }
  81. /*
  82. * Bump the I/O in flight count on the buftarg if we haven't yet done so for
  83. * this buffer. The count is incremented once per buffer (per hold cycle)
  84. * because the corresponding decrement is deferred to buffer release. Buffers
  85. * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
  86. * tracking adds unnecessary overhead. This is used for sychronization purposes
  87. * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
  88. * in-flight buffers.
  89. *
  90. * Buffers that are never released (e.g., superblock, iclog buffers) must set
  91. * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
  92. * never reaches zero and unmount hangs indefinitely.
  93. */
  94. static inline void
  95. xfs_buf_ioacct_inc(
  96. struct xfs_buf *bp)
  97. {
  98. if (bp->b_flags & XBF_NO_IOACCT)
  99. return;
  100. ASSERT(bp->b_flags & XBF_ASYNC);
  101. spin_lock(&bp->b_lock);
  102. if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
  103. bp->b_state |= XFS_BSTATE_IN_FLIGHT;
  104. percpu_counter_inc(&bp->b_target->bt_io_count);
  105. }
  106. spin_unlock(&bp->b_lock);
  107. }
  108. /*
  109. * Clear the in-flight state on a buffer about to be released to the LRU or
  110. * freed and unaccount from the buftarg.
  111. */
  112. static inline void
  113. __xfs_buf_ioacct_dec(
  114. struct xfs_buf *bp)
  115. {
  116. lockdep_assert_held(&bp->b_lock);
  117. if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
  118. bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
  119. percpu_counter_dec(&bp->b_target->bt_io_count);
  120. }
  121. }
  122. static inline void
  123. xfs_buf_ioacct_dec(
  124. struct xfs_buf *bp)
  125. {
  126. spin_lock(&bp->b_lock);
  127. __xfs_buf_ioacct_dec(bp);
  128. spin_unlock(&bp->b_lock);
  129. }
  130. /*
  131. * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  132. * b_lru_ref count so that the buffer is freed immediately when the buffer
  133. * reference count falls to zero. If the buffer is already on the LRU, we need
  134. * to remove the reference that LRU holds on the buffer.
  135. *
  136. * This prevents build-up of stale buffers on the LRU.
  137. */
  138. void
  139. xfs_buf_stale(
  140. struct xfs_buf *bp)
  141. {
  142. ASSERT(xfs_buf_islocked(bp));
  143. bp->b_flags |= XBF_STALE;
  144. /*
  145. * Clear the delwri status so that a delwri queue walker will not
  146. * flush this buffer to disk now that it is stale. The delwri queue has
  147. * a reference to the buffer, so this is safe to do.
  148. */
  149. bp->b_flags &= ~_XBF_DELWRI_Q;
  150. /*
  151. * Once the buffer is marked stale and unlocked, a subsequent lookup
  152. * could reset b_flags. There is no guarantee that the buffer is
  153. * unaccounted (released to LRU) before that occurs. Drop in-flight
  154. * status now to preserve accounting consistency.
  155. */
  156. spin_lock(&bp->b_lock);
  157. __xfs_buf_ioacct_dec(bp);
  158. atomic_set(&bp->b_lru_ref, 0);
  159. if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
  160. (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
  161. atomic_dec(&bp->b_hold);
  162. ASSERT(atomic_read(&bp->b_hold) >= 1);
  163. spin_unlock(&bp->b_lock);
  164. }
  165. static int
  166. xfs_buf_get_maps(
  167. struct xfs_buf *bp,
  168. int map_count)
  169. {
  170. ASSERT(bp->b_maps == NULL);
  171. bp->b_map_count = map_count;
  172. if (map_count == 1) {
  173. bp->b_maps = &bp->__b_map;
  174. return 0;
  175. }
  176. bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
  177. KM_NOFS);
  178. if (!bp->b_maps)
  179. return -ENOMEM;
  180. return 0;
  181. }
  182. /*
  183. * Frees b_pages if it was allocated.
  184. */
  185. static void
  186. xfs_buf_free_maps(
  187. struct xfs_buf *bp)
  188. {
  189. if (bp->b_maps != &bp->__b_map) {
  190. kmem_free(bp->b_maps);
  191. bp->b_maps = NULL;
  192. }
  193. }
  194. struct xfs_buf *
  195. _xfs_buf_alloc(
  196. struct xfs_buftarg *target,
  197. struct xfs_buf_map *map,
  198. int nmaps,
  199. xfs_buf_flags_t flags)
  200. {
  201. struct xfs_buf *bp;
  202. int error;
  203. int i;
  204. bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
  205. if (unlikely(!bp))
  206. return NULL;
  207. /*
  208. * We don't want certain flags to appear in b_flags unless they are
  209. * specifically set by later operations on the buffer.
  210. */
  211. flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
  212. atomic_set(&bp->b_hold, 1);
  213. atomic_set(&bp->b_lru_ref, 1);
  214. init_completion(&bp->b_iowait);
  215. INIT_LIST_HEAD(&bp->b_lru);
  216. INIT_LIST_HEAD(&bp->b_list);
  217. INIT_LIST_HEAD(&bp->b_li_list);
  218. sema_init(&bp->b_sema, 0); /* held, no waiters */
  219. spin_lock_init(&bp->b_lock);
  220. bp->b_target = target;
  221. bp->b_flags = flags;
  222. /*
  223. * Set length and io_length to the same value initially.
  224. * I/O routines should use io_length, which will be the same in
  225. * most cases but may be reset (e.g. XFS recovery).
  226. */
  227. error = xfs_buf_get_maps(bp, nmaps);
  228. if (error) {
  229. kmem_zone_free(xfs_buf_zone, bp);
  230. return NULL;
  231. }
  232. bp->b_bn = map[0].bm_bn;
  233. bp->b_length = 0;
  234. for (i = 0; i < nmaps; i++) {
  235. bp->b_maps[i].bm_bn = map[i].bm_bn;
  236. bp->b_maps[i].bm_len = map[i].bm_len;
  237. bp->b_length += map[i].bm_len;
  238. }
  239. bp->b_io_length = bp->b_length;
  240. atomic_set(&bp->b_pin_count, 0);
  241. init_waitqueue_head(&bp->b_waiters);
  242. XFS_STATS_INC(target->bt_mount, xb_create);
  243. trace_xfs_buf_init(bp, _RET_IP_);
  244. return bp;
  245. }
  246. /*
  247. * Allocate a page array capable of holding a specified number
  248. * of pages, and point the page buf at it.
  249. */
  250. STATIC int
  251. _xfs_buf_get_pages(
  252. xfs_buf_t *bp,
  253. int page_count)
  254. {
  255. /* Make sure that we have a page list */
  256. if (bp->b_pages == NULL) {
  257. bp->b_page_count = page_count;
  258. if (page_count <= XB_PAGES) {
  259. bp->b_pages = bp->b_page_array;
  260. } else {
  261. bp->b_pages = kmem_alloc(sizeof(struct page *) *
  262. page_count, KM_NOFS);
  263. if (bp->b_pages == NULL)
  264. return -ENOMEM;
  265. }
  266. memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
  267. }
  268. return 0;
  269. }
  270. /*
  271. * Frees b_pages if it was allocated.
  272. */
  273. STATIC void
  274. _xfs_buf_free_pages(
  275. xfs_buf_t *bp)
  276. {
  277. if (bp->b_pages != bp->b_page_array) {
  278. kmem_free(bp->b_pages);
  279. bp->b_pages = NULL;
  280. }
  281. }
  282. /*
  283. * Releases the specified buffer.
  284. *
  285. * The modification state of any associated pages is left unchanged.
  286. * The buffer must not be on any hash - use xfs_buf_rele instead for
  287. * hashed and refcounted buffers
  288. */
  289. void
  290. xfs_buf_free(
  291. xfs_buf_t *bp)
  292. {
  293. trace_xfs_buf_free(bp, _RET_IP_);
  294. ASSERT(list_empty(&bp->b_lru));
  295. if (bp->b_flags & _XBF_PAGES) {
  296. uint i;
  297. if (xfs_buf_is_vmapped(bp))
  298. vm_unmap_ram(bp->b_addr - bp->b_offset,
  299. bp->b_page_count);
  300. for (i = 0; i < bp->b_page_count; i++) {
  301. struct page *page = bp->b_pages[i];
  302. __free_page(page);
  303. }
  304. } else if (bp->b_flags & _XBF_KMEM)
  305. kmem_free(bp->b_addr);
  306. _xfs_buf_free_pages(bp);
  307. xfs_buf_free_maps(bp);
  308. kmem_zone_free(xfs_buf_zone, bp);
  309. }
  310. /*
  311. * Allocates all the pages for buffer in question and builds it's page list.
  312. */
  313. STATIC int
  314. xfs_buf_allocate_memory(
  315. xfs_buf_t *bp,
  316. uint flags)
  317. {
  318. size_t size;
  319. size_t nbytes, offset;
  320. gfp_t gfp_mask = xb_to_gfp(flags);
  321. unsigned short page_count, i;
  322. xfs_off_t start, end;
  323. int error;
  324. /*
  325. * for buffers that are contained within a single page, just allocate
  326. * the memory from the heap - there's no need for the complexity of
  327. * page arrays to keep allocation down to order 0.
  328. */
  329. size = BBTOB(bp->b_length);
  330. if (size < PAGE_SIZE) {
  331. bp->b_addr = kmem_alloc(size, KM_NOFS);
  332. if (!bp->b_addr) {
  333. /* low memory - use alloc_page loop instead */
  334. goto use_alloc_page;
  335. }
  336. if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
  337. ((unsigned long)bp->b_addr & PAGE_MASK)) {
  338. /* b_addr spans two pages - use alloc_page instead */
  339. kmem_free(bp->b_addr);
  340. bp->b_addr = NULL;
  341. goto use_alloc_page;
  342. }
  343. bp->b_offset = offset_in_page(bp->b_addr);
  344. bp->b_pages = bp->b_page_array;
  345. bp->b_pages[0] = virt_to_page(bp->b_addr);
  346. bp->b_page_count = 1;
  347. bp->b_flags |= _XBF_KMEM;
  348. return 0;
  349. }
  350. use_alloc_page:
  351. start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
  352. end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
  353. >> PAGE_SHIFT;
  354. page_count = end - start;
  355. error = _xfs_buf_get_pages(bp, page_count);
  356. if (unlikely(error))
  357. return error;
  358. offset = bp->b_offset;
  359. bp->b_flags |= _XBF_PAGES;
  360. for (i = 0; i < bp->b_page_count; i++) {
  361. struct page *page;
  362. uint retries = 0;
  363. retry:
  364. page = alloc_page(gfp_mask);
  365. if (unlikely(page == NULL)) {
  366. if (flags & XBF_READ_AHEAD) {
  367. bp->b_page_count = i;
  368. error = -ENOMEM;
  369. goto out_free_pages;
  370. }
  371. /*
  372. * This could deadlock.
  373. *
  374. * But until all the XFS lowlevel code is revamped to
  375. * handle buffer allocation failures we can't do much.
  376. */
  377. if (!(++retries % 100))
  378. xfs_err(NULL,
  379. "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
  380. current->comm, current->pid,
  381. __func__, gfp_mask);
  382. XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries);
  383. congestion_wait(BLK_RW_ASYNC, HZ/50);
  384. goto retry;
  385. }
  386. XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found);
  387. nbytes = min_t(size_t, size, PAGE_SIZE - offset);
  388. size -= nbytes;
  389. bp->b_pages[i] = page;
  390. offset = 0;
  391. }
  392. return 0;
  393. out_free_pages:
  394. for (i = 0; i < bp->b_page_count; i++)
  395. __free_page(bp->b_pages[i]);
  396. bp->b_flags &= ~_XBF_PAGES;
  397. return error;
  398. }
  399. /*
  400. * Map buffer into kernel address-space if necessary.
  401. */
  402. STATIC int
  403. _xfs_buf_map_pages(
  404. xfs_buf_t *bp,
  405. uint flags)
  406. {
  407. ASSERT(bp->b_flags & _XBF_PAGES);
  408. if (bp->b_page_count == 1) {
  409. /* A single page buffer is always mappable */
  410. bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
  411. } else if (flags & XBF_UNMAPPED) {
  412. bp->b_addr = NULL;
  413. } else {
  414. int retried = 0;
  415. unsigned nofs_flag;
  416. /*
  417. * vm_map_ram() will allocate auxillary structures (e.g.
  418. * pagetables) with GFP_KERNEL, yet we are likely to be under
  419. * GFP_NOFS context here. Hence we need to tell memory reclaim
  420. * that we are in such a context via PF_MEMALLOC_NOFS to prevent
  421. * memory reclaim re-entering the filesystem here and
  422. * potentially deadlocking.
  423. */
  424. nofs_flag = memalloc_nofs_save();
  425. do {
  426. bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
  427. -1, PAGE_KERNEL);
  428. if (bp->b_addr)
  429. break;
  430. vm_unmap_aliases();
  431. } while (retried++ <= 1);
  432. memalloc_nofs_restore(nofs_flag);
  433. if (!bp->b_addr)
  434. return -ENOMEM;
  435. bp->b_addr += bp->b_offset;
  436. }
  437. return 0;
  438. }
  439. /*
  440. * Finding and Reading Buffers
  441. */
  442. static int
  443. _xfs_buf_obj_cmp(
  444. struct rhashtable_compare_arg *arg,
  445. const void *obj)
  446. {
  447. const struct xfs_buf_map *map = arg->key;
  448. const struct xfs_buf *bp = obj;
  449. /*
  450. * The key hashing in the lookup path depends on the key being the
  451. * first element of the compare_arg, make sure to assert this.
  452. */
  453. BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
  454. if (bp->b_bn != map->bm_bn)
  455. return 1;
  456. if (unlikely(bp->b_length != map->bm_len)) {
  457. /*
  458. * found a block number match. If the range doesn't
  459. * match, the only way this is allowed is if the buffer
  460. * in the cache is stale and the transaction that made
  461. * it stale has not yet committed. i.e. we are
  462. * reallocating a busy extent. Skip this buffer and
  463. * continue searching for an exact match.
  464. */
  465. ASSERT(bp->b_flags & XBF_STALE);
  466. return 1;
  467. }
  468. return 0;
  469. }
  470. static const struct rhashtable_params xfs_buf_hash_params = {
  471. .min_size = 32, /* empty AGs have minimal footprint */
  472. .nelem_hint = 16,
  473. .key_len = sizeof(xfs_daddr_t),
  474. .key_offset = offsetof(struct xfs_buf, b_bn),
  475. .head_offset = offsetof(struct xfs_buf, b_rhash_head),
  476. .automatic_shrinking = true,
  477. .obj_cmpfn = _xfs_buf_obj_cmp,
  478. };
  479. int
  480. xfs_buf_hash_init(
  481. struct xfs_perag *pag)
  482. {
  483. spin_lock_init(&pag->pag_buf_lock);
  484. return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
  485. }
  486. void
  487. xfs_buf_hash_destroy(
  488. struct xfs_perag *pag)
  489. {
  490. rhashtable_destroy(&pag->pag_buf_hash);
  491. }
  492. /*
  493. * Look up a buffer in the buffer cache and return it referenced and locked
  494. * in @found_bp.
  495. *
  496. * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
  497. * cache.
  498. *
  499. * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
  500. * -EAGAIN if we fail to lock it.
  501. *
  502. * Return values are:
  503. * -EFSCORRUPTED if have been supplied with an invalid address
  504. * -EAGAIN on trylock failure
  505. * -ENOENT if we fail to find a match and @new_bp was NULL
  506. * 0, with @found_bp:
  507. * - @new_bp if we inserted it into the cache
  508. * - the buffer we found and locked.
  509. */
  510. static int
  511. xfs_buf_find(
  512. struct xfs_buftarg *btp,
  513. struct xfs_buf_map *map,
  514. int nmaps,
  515. xfs_buf_flags_t flags,
  516. struct xfs_buf *new_bp,
  517. struct xfs_buf **found_bp)
  518. {
  519. struct xfs_perag *pag;
  520. xfs_buf_t *bp;
  521. struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
  522. xfs_daddr_t eofs;
  523. int i;
  524. *found_bp = NULL;
  525. for (i = 0; i < nmaps; i++)
  526. cmap.bm_len += map[i].bm_len;
  527. /* Check for IOs smaller than the sector size / not sector aligned */
  528. ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
  529. ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
  530. /*
  531. * Corrupted block numbers can get through to here, unfortunately, so we
  532. * have to check that the buffer falls within the filesystem bounds.
  533. */
  534. eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
  535. if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
  536. xfs_alert(btp->bt_mount,
  537. "%s: daddr 0x%llx out of range, EOFS 0x%llx",
  538. __func__, cmap.bm_bn, eofs);
  539. WARN_ON(1);
  540. return -EFSCORRUPTED;
  541. }
  542. pag = xfs_perag_get(btp->bt_mount,
  543. xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
  544. spin_lock(&pag->pag_buf_lock);
  545. bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
  546. xfs_buf_hash_params);
  547. if (bp) {
  548. atomic_inc(&bp->b_hold);
  549. goto found;
  550. }
  551. /* No match found */
  552. if (!new_bp) {
  553. XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
  554. spin_unlock(&pag->pag_buf_lock);
  555. xfs_perag_put(pag);
  556. return -ENOENT;
  557. }
  558. /* the buffer keeps the perag reference until it is freed */
  559. new_bp->b_pag = pag;
  560. rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
  561. xfs_buf_hash_params);
  562. spin_unlock(&pag->pag_buf_lock);
  563. *found_bp = new_bp;
  564. return 0;
  565. found:
  566. spin_unlock(&pag->pag_buf_lock);
  567. xfs_perag_put(pag);
  568. if (!xfs_buf_trylock(bp)) {
  569. if (flags & XBF_TRYLOCK) {
  570. xfs_buf_rele(bp);
  571. XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
  572. return -EAGAIN;
  573. }
  574. xfs_buf_lock(bp);
  575. XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
  576. }
  577. /*
  578. * if the buffer is stale, clear all the external state associated with
  579. * it. We need to keep flags such as how we allocated the buffer memory
  580. * intact here.
  581. */
  582. if (bp->b_flags & XBF_STALE) {
  583. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  584. ASSERT(bp->b_iodone == NULL);
  585. bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
  586. bp->b_ops = NULL;
  587. }
  588. trace_xfs_buf_find(bp, flags, _RET_IP_);
  589. XFS_STATS_INC(btp->bt_mount, xb_get_locked);
  590. *found_bp = bp;
  591. return 0;
  592. }
  593. struct xfs_buf *
  594. xfs_buf_incore(
  595. struct xfs_buftarg *target,
  596. xfs_daddr_t blkno,
  597. size_t numblks,
  598. xfs_buf_flags_t flags)
  599. {
  600. struct xfs_buf *bp;
  601. int error;
  602. DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
  603. error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
  604. if (error)
  605. return NULL;
  606. return bp;
  607. }
  608. /*
  609. * Assembles a buffer covering the specified range. The code is optimised for
  610. * cache hits, as metadata intensive workloads will see 3 orders of magnitude
  611. * more hits than misses.
  612. */
  613. struct xfs_buf *
  614. xfs_buf_get_map(
  615. struct xfs_buftarg *target,
  616. struct xfs_buf_map *map,
  617. int nmaps,
  618. xfs_buf_flags_t flags)
  619. {
  620. struct xfs_buf *bp;
  621. struct xfs_buf *new_bp;
  622. int error = 0;
  623. error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
  624. switch (error) {
  625. case 0:
  626. /* cache hit */
  627. goto found;
  628. case -EAGAIN:
  629. /* cache hit, trylock failure, caller handles failure */
  630. ASSERT(flags & XBF_TRYLOCK);
  631. return NULL;
  632. case -ENOENT:
  633. /* cache miss, go for insert */
  634. break;
  635. case -EFSCORRUPTED:
  636. default:
  637. /*
  638. * None of the higher layers understand failure types
  639. * yet, so return NULL to signal a fatal lookup error.
  640. */
  641. return NULL;
  642. }
  643. new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
  644. if (unlikely(!new_bp))
  645. return NULL;
  646. error = xfs_buf_allocate_memory(new_bp, flags);
  647. if (error) {
  648. xfs_buf_free(new_bp);
  649. return NULL;
  650. }
  651. error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
  652. if (error) {
  653. xfs_buf_free(new_bp);
  654. return NULL;
  655. }
  656. if (bp != new_bp)
  657. xfs_buf_free(new_bp);
  658. found:
  659. if (!bp->b_addr) {
  660. error = _xfs_buf_map_pages(bp, flags);
  661. if (unlikely(error)) {
  662. xfs_warn(target->bt_mount,
  663. "%s: failed to map pagesn", __func__);
  664. xfs_buf_relse(bp);
  665. return NULL;
  666. }
  667. }
  668. /*
  669. * Clear b_error if this is a lookup from a caller that doesn't expect
  670. * valid data to be found in the buffer.
  671. */
  672. if (!(flags & XBF_READ))
  673. xfs_buf_ioerror(bp, 0);
  674. XFS_STATS_INC(target->bt_mount, xb_get);
  675. trace_xfs_buf_get(bp, flags, _RET_IP_);
  676. return bp;
  677. }
  678. STATIC int
  679. _xfs_buf_read(
  680. xfs_buf_t *bp,
  681. xfs_buf_flags_t flags)
  682. {
  683. ASSERT(!(flags & XBF_WRITE));
  684. ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
  685. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
  686. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
  687. return xfs_buf_submit(bp);
  688. }
  689. xfs_buf_t *
  690. xfs_buf_read_map(
  691. struct xfs_buftarg *target,
  692. struct xfs_buf_map *map,
  693. int nmaps,
  694. xfs_buf_flags_t flags,
  695. const struct xfs_buf_ops *ops)
  696. {
  697. struct xfs_buf *bp;
  698. flags |= XBF_READ;
  699. bp = xfs_buf_get_map(target, map, nmaps, flags);
  700. if (bp) {
  701. trace_xfs_buf_read(bp, flags, _RET_IP_);
  702. if (!(bp->b_flags & XBF_DONE)) {
  703. XFS_STATS_INC(target->bt_mount, xb_get_read);
  704. bp->b_ops = ops;
  705. _xfs_buf_read(bp, flags);
  706. } else if (flags & XBF_ASYNC) {
  707. /*
  708. * Read ahead call which is already satisfied,
  709. * drop the buffer
  710. */
  711. xfs_buf_relse(bp);
  712. return NULL;
  713. } else {
  714. /* We do not want read in the flags */
  715. bp->b_flags &= ~XBF_READ;
  716. }
  717. }
  718. return bp;
  719. }
  720. /*
  721. * If we are not low on memory then do the readahead in a deadlock
  722. * safe manner.
  723. */
  724. void
  725. xfs_buf_readahead_map(
  726. struct xfs_buftarg *target,
  727. struct xfs_buf_map *map,
  728. int nmaps,
  729. const struct xfs_buf_ops *ops)
  730. {
  731. if (bdi_read_congested(target->bt_bdev->bd_bdi))
  732. return;
  733. xfs_buf_read_map(target, map, nmaps,
  734. XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
  735. }
  736. /*
  737. * Read an uncached buffer from disk. Allocates and returns a locked
  738. * buffer containing the disk contents or nothing.
  739. */
  740. int
  741. xfs_buf_read_uncached(
  742. struct xfs_buftarg *target,
  743. xfs_daddr_t daddr,
  744. size_t numblks,
  745. int flags,
  746. struct xfs_buf **bpp,
  747. const struct xfs_buf_ops *ops)
  748. {
  749. struct xfs_buf *bp;
  750. *bpp = NULL;
  751. bp = xfs_buf_get_uncached(target, numblks, flags);
  752. if (!bp)
  753. return -ENOMEM;
  754. /* set up the buffer for a read IO */
  755. ASSERT(bp->b_map_count == 1);
  756. bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
  757. bp->b_maps[0].bm_bn = daddr;
  758. bp->b_flags |= XBF_READ;
  759. bp->b_ops = ops;
  760. xfs_buf_submit(bp);
  761. if (bp->b_error) {
  762. int error = bp->b_error;
  763. xfs_buf_relse(bp);
  764. return error;
  765. }
  766. *bpp = bp;
  767. return 0;
  768. }
  769. /*
  770. * Return a buffer allocated as an empty buffer and associated to external
  771. * memory via xfs_buf_associate_memory() back to it's empty state.
  772. */
  773. void
  774. xfs_buf_set_empty(
  775. struct xfs_buf *bp,
  776. size_t numblks)
  777. {
  778. if (bp->b_pages)
  779. _xfs_buf_free_pages(bp);
  780. bp->b_pages = NULL;
  781. bp->b_page_count = 0;
  782. bp->b_addr = NULL;
  783. bp->b_length = numblks;
  784. bp->b_io_length = numblks;
  785. ASSERT(bp->b_map_count == 1);
  786. bp->b_bn = XFS_BUF_DADDR_NULL;
  787. bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
  788. bp->b_maps[0].bm_len = bp->b_length;
  789. }
  790. static inline struct page *
  791. mem_to_page(
  792. void *addr)
  793. {
  794. if ((!is_vmalloc_addr(addr))) {
  795. return virt_to_page(addr);
  796. } else {
  797. return vmalloc_to_page(addr);
  798. }
  799. }
  800. int
  801. xfs_buf_associate_memory(
  802. xfs_buf_t *bp,
  803. void *mem,
  804. size_t len)
  805. {
  806. int rval;
  807. int i = 0;
  808. unsigned long pageaddr;
  809. unsigned long offset;
  810. size_t buflen;
  811. int page_count;
  812. pageaddr = (unsigned long)mem & PAGE_MASK;
  813. offset = (unsigned long)mem - pageaddr;
  814. buflen = PAGE_ALIGN(len + offset);
  815. page_count = buflen >> PAGE_SHIFT;
  816. /* Free any previous set of page pointers */
  817. if (bp->b_pages)
  818. _xfs_buf_free_pages(bp);
  819. bp->b_pages = NULL;
  820. bp->b_addr = mem;
  821. rval = _xfs_buf_get_pages(bp, page_count);
  822. if (rval)
  823. return rval;
  824. bp->b_offset = offset;
  825. for (i = 0; i < bp->b_page_count; i++) {
  826. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  827. pageaddr += PAGE_SIZE;
  828. }
  829. bp->b_io_length = BTOBB(len);
  830. bp->b_length = BTOBB(buflen);
  831. return 0;
  832. }
  833. xfs_buf_t *
  834. xfs_buf_get_uncached(
  835. struct xfs_buftarg *target,
  836. size_t numblks,
  837. int flags)
  838. {
  839. unsigned long page_count;
  840. int error, i;
  841. struct xfs_buf *bp;
  842. DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
  843. /* flags might contain irrelevant bits, pass only what we care about */
  844. bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
  845. if (unlikely(bp == NULL))
  846. goto fail;
  847. page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
  848. error = _xfs_buf_get_pages(bp, page_count);
  849. if (error)
  850. goto fail_free_buf;
  851. for (i = 0; i < page_count; i++) {
  852. bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
  853. if (!bp->b_pages[i])
  854. goto fail_free_mem;
  855. }
  856. bp->b_flags |= _XBF_PAGES;
  857. error = _xfs_buf_map_pages(bp, 0);
  858. if (unlikely(error)) {
  859. xfs_warn(target->bt_mount,
  860. "%s: failed to map pages", __func__);
  861. goto fail_free_mem;
  862. }
  863. trace_xfs_buf_get_uncached(bp, _RET_IP_);
  864. return bp;
  865. fail_free_mem:
  866. while (--i >= 0)
  867. __free_page(bp->b_pages[i]);
  868. _xfs_buf_free_pages(bp);
  869. fail_free_buf:
  870. xfs_buf_free_maps(bp);
  871. kmem_zone_free(xfs_buf_zone, bp);
  872. fail:
  873. return NULL;
  874. }
  875. /*
  876. * Increment reference count on buffer, to hold the buffer concurrently
  877. * with another thread which may release (free) the buffer asynchronously.
  878. * Must hold the buffer already to call this function.
  879. */
  880. void
  881. xfs_buf_hold(
  882. xfs_buf_t *bp)
  883. {
  884. trace_xfs_buf_hold(bp, _RET_IP_);
  885. atomic_inc(&bp->b_hold);
  886. }
  887. /*
  888. * Release a hold on the specified buffer. If the hold count is 1, the buffer is
  889. * placed on LRU or freed (depending on b_lru_ref).
  890. */
  891. void
  892. xfs_buf_rele(
  893. xfs_buf_t *bp)
  894. {
  895. struct xfs_perag *pag = bp->b_pag;
  896. bool release;
  897. bool freebuf = false;
  898. trace_xfs_buf_rele(bp, _RET_IP_);
  899. if (!pag) {
  900. ASSERT(list_empty(&bp->b_lru));
  901. if (atomic_dec_and_test(&bp->b_hold)) {
  902. xfs_buf_ioacct_dec(bp);
  903. xfs_buf_free(bp);
  904. }
  905. return;
  906. }
  907. ASSERT(atomic_read(&bp->b_hold) > 0);
  908. /*
  909. * We grab the b_lock here first to serialise racing xfs_buf_rele()
  910. * calls. The pag_buf_lock being taken on the last reference only
  911. * serialises against racing lookups in xfs_buf_find(). IOWs, the second
  912. * to last reference we drop here is not serialised against the last
  913. * reference until we take bp->b_lock. Hence if we don't grab b_lock
  914. * first, the last "release" reference can win the race to the lock and
  915. * free the buffer before the second-to-last reference is processed,
  916. * leading to a use-after-free scenario.
  917. */
  918. spin_lock(&bp->b_lock);
  919. release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
  920. if (!release) {
  921. /*
  922. * Drop the in-flight state if the buffer is already on the LRU
  923. * and it holds the only reference. This is racy because we
  924. * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
  925. * ensures the decrement occurs only once per-buf.
  926. */
  927. if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
  928. __xfs_buf_ioacct_dec(bp);
  929. goto out_unlock;
  930. }
  931. /* the last reference has been dropped ... */
  932. __xfs_buf_ioacct_dec(bp);
  933. if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
  934. /*
  935. * If the buffer is added to the LRU take a new reference to the
  936. * buffer for the LRU and clear the (now stale) dispose list
  937. * state flag
  938. */
  939. if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
  940. bp->b_state &= ~XFS_BSTATE_DISPOSE;
  941. atomic_inc(&bp->b_hold);
  942. }
  943. spin_unlock(&pag->pag_buf_lock);
  944. } else {
  945. /*
  946. * most of the time buffers will already be removed from the
  947. * LRU, so optimise that case by checking for the
  948. * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
  949. * was on was the disposal list
  950. */
  951. if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
  952. list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
  953. } else {
  954. ASSERT(list_empty(&bp->b_lru));
  955. }
  956. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  957. rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
  958. xfs_buf_hash_params);
  959. spin_unlock(&pag->pag_buf_lock);
  960. xfs_perag_put(pag);
  961. freebuf = true;
  962. }
  963. out_unlock:
  964. spin_unlock(&bp->b_lock);
  965. if (freebuf)
  966. xfs_buf_free(bp);
  967. }
  968. /*
  969. * Lock a buffer object, if it is not already locked.
  970. *
  971. * If we come across a stale, pinned, locked buffer, we know that we are
  972. * being asked to lock a buffer that has been reallocated. Because it is
  973. * pinned, we know that the log has not been pushed to disk and hence it
  974. * will still be locked. Rather than continuing to have trylock attempts
  975. * fail until someone else pushes the log, push it ourselves before
  976. * returning. This means that the xfsaild will not get stuck trying
  977. * to push on stale inode buffers.
  978. */
  979. int
  980. xfs_buf_trylock(
  981. struct xfs_buf *bp)
  982. {
  983. int locked;
  984. locked = down_trylock(&bp->b_sema) == 0;
  985. if (locked)
  986. trace_xfs_buf_trylock(bp, _RET_IP_);
  987. else
  988. trace_xfs_buf_trylock_fail(bp, _RET_IP_);
  989. return locked;
  990. }
  991. /*
  992. * Lock a buffer object.
  993. *
  994. * If we come across a stale, pinned, locked buffer, we know that we
  995. * are being asked to lock a buffer that has been reallocated. Because
  996. * it is pinned, we know that the log has not been pushed to disk and
  997. * hence it will still be locked. Rather than sleeping until someone
  998. * else pushes the log, push it ourselves before trying to get the lock.
  999. */
  1000. void
  1001. xfs_buf_lock(
  1002. struct xfs_buf *bp)
  1003. {
  1004. trace_xfs_buf_lock(bp, _RET_IP_);
  1005. if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  1006. xfs_log_force(bp->b_target->bt_mount, 0);
  1007. down(&bp->b_sema);
  1008. trace_xfs_buf_lock_done(bp, _RET_IP_);
  1009. }
  1010. void
  1011. xfs_buf_unlock(
  1012. struct xfs_buf *bp)
  1013. {
  1014. ASSERT(xfs_buf_islocked(bp));
  1015. up(&bp->b_sema);
  1016. trace_xfs_buf_unlock(bp, _RET_IP_);
  1017. }
  1018. STATIC void
  1019. xfs_buf_wait_unpin(
  1020. xfs_buf_t *bp)
  1021. {
  1022. DECLARE_WAITQUEUE (wait, current);
  1023. if (atomic_read(&bp->b_pin_count) == 0)
  1024. return;
  1025. add_wait_queue(&bp->b_waiters, &wait);
  1026. for (;;) {
  1027. set_current_state(TASK_UNINTERRUPTIBLE);
  1028. if (atomic_read(&bp->b_pin_count) == 0)
  1029. break;
  1030. io_schedule();
  1031. }
  1032. remove_wait_queue(&bp->b_waiters, &wait);
  1033. set_current_state(TASK_RUNNING);
  1034. }
  1035. /*
  1036. * Buffer Utility Routines
  1037. */
  1038. void
  1039. xfs_buf_ioend(
  1040. struct xfs_buf *bp)
  1041. {
  1042. bool read = bp->b_flags & XBF_READ;
  1043. trace_xfs_buf_iodone(bp, _RET_IP_);
  1044. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  1045. /*
  1046. * Pull in IO completion errors now. We are guaranteed to be running
  1047. * single threaded, so we don't need the lock to read b_io_error.
  1048. */
  1049. if (!bp->b_error && bp->b_io_error)
  1050. xfs_buf_ioerror(bp, bp->b_io_error);
  1051. /* Only validate buffers that were read without errors */
  1052. if (read && !bp->b_error && bp->b_ops) {
  1053. ASSERT(!bp->b_iodone);
  1054. bp->b_ops->verify_read(bp);
  1055. }
  1056. if (!bp->b_error) {
  1057. bp->b_flags &= ~XBF_WRITE_FAIL;
  1058. bp->b_flags |= XBF_DONE;
  1059. }
  1060. if (bp->b_iodone)
  1061. (*(bp->b_iodone))(bp);
  1062. else if (bp->b_flags & XBF_ASYNC)
  1063. xfs_buf_relse(bp);
  1064. else
  1065. complete(&bp->b_iowait);
  1066. }
  1067. static void
  1068. xfs_buf_ioend_work(
  1069. struct work_struct *work)
  1070. {
  1071. struct xfs_buf *bp =
  1072. container_of(work, xfs_buf_t, b_ioend_work);
  1073. xfs_buf_ioend(bp);
  1074. }
  1075. static void
  1076. xfs_buf_ioend_async(
  1077. struct xfs_buf *bp)
  1078. {
  1079. INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
  1080. queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
  1081. }
  1082. void
  1083. __xfs_buf_ioerror(
  1084. xfs_buf_t *bp,
  1085. int error,
  1086. xfs_failaddr_t failaddr)
  1087. {
  1088. ASSERT(error <= 0 && error >= -1000);
  1089. bp->b_error = error;
  1090. trace_xfs_buf_ioerror(bp, error, failaddr);
  1091. }
  1092. void
  1093. xfs_buf_ioerror_alert(
  1094. struct xfs_buf *bp,
  1095. const char *func)
  1096. {
  1097. xfs_alert(bp->b_target->bt_mount,
  1098. "metadata I/O error in \"%s\" at daddr 0x%llx len %d error %d",
  1099. func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length,
  1100. -bp->b_error);
  1101. }
  1102. int
  1103. xfs_bwrite(
  1104. struct xfs_buf *bp)
  1105. {
  1106. int error;
  1107. ASSERT(xfs_buf_islocked(bp));
  1108. bp->b_flags |= XBF_WRITE;
  1109. bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
  1110. XBF_DONE);
  1111. error = xfs_buf_submit(bp);
  1112. if (error) {
  1113. xfs_force_shutdown(bp->b_target->bt_mount,
  1114. SHUTDOWN_META_IO_ERROR);
  1115. }
  1116. return error;
  1117. }
  1118. static void
  1119. xfs_buf_bio_end_io(
  1120. struct bio *bio)
  1121. {
  1122. struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
  1123. /*
  1124. * don't overwrite existing errors - otherwise we can lose errors on
  1125. * buffers that require multiple bios to complete.
  1126. */
  1127. if (bio->bi_status) {
  1128. int error = blk_status_to_errno(bio->bi_status);
  1129. cmpxchg(&bp->b_io_error, 0, error);
  1130. }
  1131. if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
  1132. invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
  1133. if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
  1134. xfs_buf_ioend_async(bp);
  1135. bio_put(bio);
  1136. }
  1137. static void
  1138. xfs_buf_ioapply_map(
  1139. struct xfs_buf *bp,
  1140. int map,
  1141. int *buf_offset,
  1142. int *count,
  1143. int op,
  1144. int op_flags)
  1145. {
  1146. int page_index;
  1147. int total_nr_pages = bp->b_page_count;
  1148. int nr_pages;
  1149. struct bio *bio;
  1150. sector_t sector = bp->b_maps[map].bm_bn;
  1151. int size;
  1152. int offset;
  1153. /* skip the pages in the buffer before the start offset */
  1154. page_index = 0;
  1155. offset = *buf_offset;
  1156. while (offset >= PAGE_SIZE) {
  1157. page_index++;
  1158. offset -= PAGE_SIZE;
  1159. }
  1160. /*
  1161. * Limit the IO size to the length of the current vector, and update the
  1162. * remaining IO count for the next time around.
  1163. */
  1164. size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
  1165. *count -= size;
  1166. *buf_offset += size;
  1167. next_chunk:
  1168. atomic_inc(&bp->b_io_remaining);
  1169. nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
  1170. bio = bio_alloc(GFP_NOIO, nr_pages);
  1171. bio_set_dev(bio, bp->b_target->bt_bdev);
  1172. bio->bi_iter.bi_sector = sector;
  1173. bio->bi_end_io = xfs_buf_bio_end_io;
  1174. bio->bi_private = bp;
  1175. bio_set_op_attrs(bio, op, op_flags);
  1176. for (; size && nr_pages; nr_pages--, page_index++) {
  1177. int rbytes, nbytes = PAGE_SIZE - offset;
  1178. if (nbytes > size)
  1179. nbytes = size;
  1180. rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
  1181. offset);
  1182. if (rbytes < nbytes)
  1183. break;
  1184. offset = 0;
  1185. sector += BTOBB(nbytes);
  1186. size -= nbytes;
  1187. total_nr_pages--;
  1188. }
  1189. if (likely(bio->bi_iter.bi_size)) {
  1190. if (xfs_buf_is_vmapped(bp)) {
  1191. flush_kernel_vmap_range(bp->b_addr,
  1192. xfs_buf_vmap_len(bp));
  1193. }
  1194. submit_bio(bio);
  1195. if (size)
  1196. goto next_chunk;
  1197. } else {
  1198. /*
  1199. * This is guaranteed not to be the last io reference count
  1200. * because the caller (xfs_buf_submit) holds a count itself.
  1201. */
  1202. atomic_dec(&bp->b_io_remaining);
  1203. xfs_buf_ioerror(bp, -EIO);
  1204. bio_put(bio);
  1205. }
  1206. }
  1207. STATIC void
  1208. _xfs_buf_ioapply(
  1209. struct xfs_buf *bp)
  1210. {
  1211. struct blk_plug plug;
  1212. int op;
  1213. int op_flags = 0;
  1214. int offset;
  1215. int size;
  1216. int i;
  1217. /*
  1218. * Make sure we capture only current IO errors rather than stale errors
  1219. * left over from previous use of the buffer (e.g. failed readahead).
  1220. */
  1221. bp->b_error = 0;
  1222. /*
  1223. * Initialize the I/O completion workqueue if we haven't yet or the
  1224. * submitter has not opted to specify a custom one.
  1225. */
  1226. if (!bp->b_ioend_wq)
  1227. bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
  1228. if (bp->b_flags & XBF_WRITE) {
  1229. op = REQ_OP_WRITE;
  1230. if (bp->b_flags & XBF_SYNCIO)
  1231. op_flags = REQ_SYNC;
  1232. if (bp->b_flags & XBF_FUA)
  1233. op_flags |= REQ_FUA;
  1234. if (bp->b_flags & XBF_FLUSH)
  1235. op_flags |= REQ_PREFLUSH;
  1236. /*
  1237. * Run the write verifier callback function if it exists. If
  1238. * this function fails it will mark the buffer with an error and
  1239. * the IO should not be dispatched.
  1240. */
  1241. if (bp->b_ops) {
  1242. bp->b_ops->verify_write(bp);
  1243. if (bp->b_error) {
  1244. xfs_force_shutdown(bp->b_target->bt_mount,
  1245. SHUTDOWN_CORRUPT_INCORE);
  1246. return;
  1247. }
  1248. } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
  1249. struct xfs_mount *mp = bp->b_target->bt_mount;
  1250. /*
  1251. * non-crc filesystems don't attach verifiers during
  1252. * log recovery, so don't warn for such filesystems.
  1253. */
  1254. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  1255. xfs_warn(mp,
  1256. "%s: no buf ops on daddr 0x%llx len %d",
  1257. __func__, bp->b_bn, bp->b_length);
  1258. xfs_hex_dump(bp->b_addr,
  1259. XFS_CORRUPTION_DUMP_LEN);
  1260. dump_stack();
  1261. }
  1262. }
  1263. } else if (bp->b_flags & XBF_READ_AHEAD) {
  1264. op = REQ_OP_READ;
  1265. op_flags = REQ_RAHEAD;
  1266. } else {
  1267. op = REQ_OP_READ;
  1268. }
  1269. /* we only use the buffer cache for meta-data */
  1270. op_flags |= REQ_META;
  1271. /*
  1272. * Walk all the vectors issuing IO on them. Set up the initial offset
  1273. * into the buffer and the desired IO size before we start -
  1274. * _xfs_buf_ioapply_vec() will modify them appropriately for each
  1275. * subsequent call.
  1276. */
  1277. offset = bp->b_offset;
  1278. size = BBTOB(bp->b_io_length);
  1279. blk_start_plug(&plug);
  1280. for (i = 0; i < bp->b_map_count; i++) {
  1281. xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
  1282. if (bp->b_error)
  1283. break;
  1284. if (size <= 0)
  1285. break; /* all done */
  1286. }
  1287. blk_finish_plug(&plug);
  1288. }
  1289. /*
  1290. * Wait for I/O completion of a sync buffer and return the I/O error code.
  1291. */
  1292. static int
  1293. xfs_buf_iowait(
  1294. struct xfs_buf *bp)
  1295. {
  1296. ASSERT(!(bp->b_flags & XBF_ASYNC));
  1297. trace_xfs_buf_iowait(bp, _RET_IP_);
  1298. wait_for_completion(&bp->b_iowait);
  1299. trace_xfs_buf_iowait_done(bp, _RET_IP_);
  1300. return bp->b_error;
  1301. }
  1302. /*
  1303. * Buffer I/O submission path, read or write. Asynchronous submission transfers
  1304. * the buffer lock ownership and the current reference to the IO. It is not
  1305. * safe to reference the buffer after a call to this function unless the caller
  1306. * holds an additional reference itself.
  1307. */
  1308. int
  1309. __xfs_buf_submit(
  1310. struct xfs_buf *bp,
  1311. bool wait)
  1312. {
  1313. int error = 0;
  1314. trace_xfs_buf_submit(bp, _RET_IP_);
  1315. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  1316. /* on shutdown we stale and complete the buffer immediately */
  1317. if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  1318. xfs_buf_ioerror(bp, -EIO);
  1319. bp->b_flags &= ~XBF_DONE;
  1320. xfs_buf_stale(bp);
  1321. xfs_buf_ioend(bp);
  1322. return -EIO;
  1323. }
  1324. /*
  1325. * Grab a reference so the buffer does not go away underneath us. For
  1326. * async buffers, I/O completion drops the callers reference, which
  1327. * could occur before submission returns.
  1328. */
  1329. xfs_buf_hold(bp);
  1330. if (bp->b_flags & XBF_WRITE)
  1331. xfs_buf_wait_unpin(bp);
  1332. /* clear the internal error state to avoid spurious errors */
  1333. bp->b_io_error = 0;
  1334. /*
  1335. * Set the count to 1 initially, this will stop an I/O completion
  1336. * callout which happens before we have started all the I/O from calling
  1337. * xfs_buf_ioend too early.
  1338. */
  1339. atomic_set(&bp->b_io_remaining, 1);
  1340. if (bp->b_flags & XBF_ASYNC)
  1341. xfs_buf_ioacct_inc(bp);
  1342. _xfs_buf_ioapply(bp);
  1343. /*
  1344. * If _xfs_buf_ioapply failed, we can get back here with only the IO
  1345. * reference we took above. If we drop it to zero, run completion so
  1346. * that we don't return to the caller with completion still pending.
  1347. */
  1348. if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
  1349. if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
  1350. xfs_buf_ioend(bp);
  1351. else
  1352. xfs_buf_ioend_async(bp);
  1353. }
  1354. if (wait)
  1355. error = xfs_buf_iowait(bp);
  1356. /*
  1357. * Release the hold that keeps the buffer referenced for the entire
  1358. * I/O. Note that if the buffer is async, it is not safe to reference
  1359. * after this release.
  1360. */
  1361. xfs_buf_rele(bp);
  1362. return error;
  1363. }
  1364. void *
  1365. xfs_buf_offset(
  1366. struct xfs_buf *bp,
  1367. size_t offset)
  1368. {
  1369. struct page *page;
  1370. if (bp->b_addr)
  1371. return bp->b_addr + offset;
  1372. offset += bp->b_offset;
  1373. page = bp->b_pages[offset >> PAGE_SHIFT];
  1374. return page_address(page) + (offset & (PAGE_SIZE-1));
  1375. }
  1376. /*
  1377. * Move data into or out of a buffer.
  1378. */
  1379. void
  1380. xfs_buf_iomove(
  1381. xfs_buf_t *bp, /* buffer to process */
  1382. size_t boff, /* starting buffer offset */
  1383. size_t bsize, /* length to copy */
  1384. void *data, /* data address */
  1385. xfs_buf_rw_t mode) /* read/write/zero flag */
  1386. {
  1387. size_t bend;
  1388. bend = boff + bsize;
  1389. while (boff < bend) {
  1390. struct page *page;
  1391. int page_index, page_offset, csize;
  1392. page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
  1393. page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
  1394. page = bp->b_pages[page_index];
  1395. csize = min_t(size_t, PAGE_SIZE - page_offset,
  1396. BBTOB(bp->b_io_length) - boff);
  1397. ASSERT((csize + page_offset) <= PAGE_SIZE);
  1398. switch (mode) {
  1399. case XBRW_ZERO:
  1400. memset(page_address(page) + page_offset, 0, csize);
  1401. break;
  1402. case XBRW_READ:
  1403. memcpy(data, page_address(page) + page_offset, csize);
  1404. break;
  1405. case XBRW_WRITE:
  1406. memcpy(page_address(page) + page_offset, data, csize);
  1407. }
  1408. boff += csize;
  1409. data += csize;
  1410. }
  1411. }
  1412. /*
  1413. * Handling of buffer targets (buftargs).
  1414. */
  1415. /*
  1416. * Wait for any bufs with callbacks that have been submitted but have not yet
  1417. * returned. These buffers will have an elevated hold count, so wait on those
  1418. * while freeing all the buffers only held by the LRU.
  1419. */
  1420. static enum lru_status
  1421. xfs_buftarg_wait_rele(
  1422. struct list_head *item,
  1423. struct list_lru_one *lru,
  1424. spinlock_t *lru_lock,
  1425. void *arg)
  1426. {
  1427. struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
  1428. struct list_head *dispose = arg;
  1429. if (atomic_read(&bp->b_hold) > 1) {
  1430. /* need to wait, so skip it this pass */
  1431. trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
  1432. return LRU_SKIP;
  1433. }
  1434. if (!spin_trylock(&bp->b_lock))
  1435. return LRU_SKIP;
  1436. /*
  1437. * clear the LRU reference count so the buffer doesn't get
  1438. * ignored in xfs_buf_rele().
  1439. */
  1440. atomic_set(&bp->b_lru_ref, 0);
  1441. bp->b_state |= XFS_BSTATE_DISPOSE;
  1442. list_lru_isolate_move(lru, item, dispose);
  1443. spin_unlock(&bp->b_lock);
  1444. return LRU_REMOVED;
  1445. }
  1446. void
  1447. xfs_wait_buftarg(
  1448. struct xfs_buftarg *btp)
  1449. {
  1450. LIST_HEAD(dispose);
  1451. int loop = 0;
  1452. /*
  1453. * First wait on the buftarg I/O count for all in-flight buffers to be
  1454. * released. This is critical as new buffers do not make the LRU until
  1455. * they are released.
  1456. *
  1457. * Next, flush the buffer workqueue to ensure all completion processing
  1458. * has finished. Just waiting on buffer locks is not sufficient for
  1459. * async IO as the reference count held over IO is not released until
  1460. * after the buffer lock is dropped. Hence we need to ensure here that
  1461. * all reference counts have been dropped before we start walking the
  1462. * LRU list.
  1463. */
  1464. while (percpu_counter_sum(&btp->bt_io_count))
  1465. delay(100);
  1466. flush_workqueue(btp->bt_mount->m_buf_workqueue);
  1467. /* loop until there is nothing left on the lru list. */
  1468. while (list_lru_count(&btp->bt_lru)) {
  1469. list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
  1470. &dispose, LONG_MAX);
  1471. while (!list_empty(&dispose)) {
  1472. struct xfs_buf *bp;
  1473. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1474. list_del_init(&bp->b_lru);
  1475. if (bp->b_flags & XBF_WRITE_FAIL) {
  1476. xfs_alert(btp->bt_mount,
  1477. "Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
  1478. (long long)bp->b_bn);
  1479. xfs_alert(btp->bt_mount,
  1480. "Please run xfs_repair to determine the extent of the problem.");
  1481. }
  1482. xfs_buf_rele(bp);
  1483. }
  1484. if (loop++ != 0)
  1485. delay(100);
  1486. }
  1487. }
  1488. static enum lru_status
  1489. xfs_buftarg_isolate(
  1490. struct list_head *item,
  1491. struct list_lru_one *lru,
  1492. spinlock_t *lru_lock,
  1493. void *arg)
  1494. {
  1495. struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
  1496. struct list_head *dispose = arg;
  1497. /*
  1498. * we are inverting the lru lock/bp->b_lock here, so use a trylock.
  1499. * If we fail to get the lock, just skip it.
  1500. */
  1501. if (!spin_trylock(&bp->b_lock))
  1502. return LRU_SKIP;
  1503. /*
  1504. * Decrement the b_lru_ref count unless the value is already
  1505. * zero. If the value is already zero, we need to reclaim the
  1506. * buffer, otherwise it gets another trip through the LRU.
  1507. */
  1508. if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
  1509. spin_unlock(&bp->b_lock);
  1510. return LRU_ROTATE;
  1511. }
  1512. bp->b_state |= XFS_BSTATE_DISPOSE;
  1513. list_lru_isolate_move(lru, item, dispose);
  1514. spin_unlock(&bp->b_lock);
  1515. return LRU_REMOVED;
  1516. }
  1517. static unsigned long
  1518. xfs_buftarg_shrink_scan(
  1519. struct shrinker *shrink,
  1520. struct shrink_control *sc)
  1521. {
  1522. struct xfs_buftarg *btp = container_of(shrink,
  1523. struct xfs_buftarg, bt_shrinker);
  1524. LIST_HEAD(dispose);
  1525. unsigned long freed;
  1526. freed = list_lru_shrink_walk(&btp->bt_lru, sc,
  1527. xfs_buftarg_isolate, &dispose);
  1528. while (!list_empty(&dispose)) {
  1529. struct xfs_buf *bp;
  1530. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1531. list_del_init(&bp->b_lru);
  1532. xfs_buf_rele(bp);
  1533. }
  1534. return freed;
  1535. }
  1536. static unsigned long
  1537. xfs_buftarg_shrink_count(
  1538. struct shrinker *shrink,
  1539. struct shrink_control *sc)
  1540. {
  1541. struct xfs_buftarg *btp = container_of(shrink,
  1542. struct xfs_buftarg, bt_shrinker);
  1543. return list_lru_shrink_count(&btp->bt_lru, sc);
  1544. }
  1545. void
  1546. xfs_free_buftarg(
  1547. struct xfs_buftarg *btp)
  1548. {
  1549. unregister_shrinker(&btp->bt_shrinker);
  1550. ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
  1551. percpu_counter_destroy(&btp->bt_io_count);
  1552. list_lru_destroy(&btp->bt_lru);
  1553. xfs_blkdev_issue_flush(btp);
  1554. kmem_free(btp);
  1555. }
  1556. int
  1557. xfs_setsize_buftarg(
  1558. xfs_buftarg_t *btp,
  1559. unsigned int sectorsize)
  1560. {
  1561. /* Set up metadata sector size info */
  1562. btp->bt_meta_sectorsize = sectorsize;
  1563. btp->bt_meta_sectormask = sectorsize - 1;
  1564. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1565. xfs_warn(btp->bt_mount,
  1566. "Cannot set_blocksize to %u on device %pg",
  1567. sectorsize, btp->bt_bdev);
  1568. return -EINVAL;
  1569. }
  1570. /* Set up device logical sector size mask */
  1571. btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
  1572. btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
  1573. return 0;
  1574. }
  1575. /*
  1576. * When allocating the initial buffer target we have not yet
  1577. * read in the superblock, so don't know what sized sectors
  1578. * are being used at this early stage. Play safe.
  1579. */
  1580. STATIC int
  1581. xfs_setsize_buftarg_early(
  1582. xfs_buftarg_t *btp,
  1583. struct block_device *bdev)
  1584. {
  1585. return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
  1586. }
  1587. xfs_buftarg_t *
  1588. xfs_alloc_buftarg(
  1589. struct xfs_mount *mp,
  1590. struct block_device *bdev,
  1591. struct dax_device *dax_dev)
  1592. {
  1593. xfs_buftarg_t *btp;
  1594. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
  1595. btp->bt_mount = mp;
  1596. btp->bt_dev = bdev->bd_dev;
  1597. btp->bt_bdev = bdev;
  1598. btp->bt_daxdev = dax_dev;
  1599. if (xfs_setsize_buftarg_early(btp, bdev))
  1600. goto error_free;
  1601. if (list_lru_init(&btp->bt_lru))
  1602. goto error_free;
  1603. if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
  1604. goto error_lru;
  1605. btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
  1606. btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
  1607. btp->bt_shrinker.seeks = DEFAULT_SEEKS;
  1608. btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
  1609. if (register_shrinker(&btp->bt_shrinker))
  1610. goto error_pcpu;
  1611. return btp;
  1612. error_pcpu:
  1613. percpu_counter_destroy(&btp->bt_io_count);
  1614. error_lru:
  1615. list_lru_destroy(&btp->bt_lru);
  1616. error_free:
  1617. kmem_free(btp);
  1618. return NULL;
  1619. }
  1620. /*
  1621. * Cancel a delayed write list.
  1622. *
  1623. * Remove each buffer from the list, clear the delwri queue flag and drop the
  1624. * associated buffer reference.
  1625. */
  1626. void
  1627. xfs_buf_delwri_cancel(
  1628. struct list_head *list)
  1629. {
  1630. struct xfs_buf *bp;
  1631. while (!list_empty(list)) {
  1632. bp = list_first_entry(list, struct xfs_buf, b_list);
  1633. xfs_buf_lock(bp);
  1634. bp->b_flags &= ~_XBF_DELWRI_Q;
  1635. list_del_init(&bp->b_list);
  1636. xfs_buf_relse(bp);
  1637. }
  1638. }
  1639. /*
  1640. * Add a buffer to the delayed write list.
  1641. *
  1642. * This queues a buffer for writeout if it hasn't already been. Note that
  1643. * neither this routine nor the buffer list submission functions perform
  1644. * any internal synchronization. It is expected that the lists are thread-local
  1645. * to the callers.
  1646. *
  1647. * Returns true if we queued up the buffer, or false if it already had
  1648. * been on the buffer list.
  1649. */
  1650. bool
  1651. xfs_buf_delwri_queue(
  1652. struct xfs_buf *bp,
  1653. struct list_head *list)
  1654. {
  1655. ASSERT(xfs_buf_islocked(bp));
  1656. ASSERT(!(bp->b_flags & XBF_READ));
  1657. /*
  1658. * If the buffer is already marked delwri it already is queued up
  1659. * by someone else for imediate writeout. Just ignore it in that
  1660. * case.
  1661. */
  1662. if (bp->b_flags & _XBF_DELWRI_Q) {
  1663. trace_xfs_buf_delwri_queued(bp, _RET_IP_);
  1664. return false;
  1665. }
  1666. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1667. /*
  1668. * If a buffer gets written out synchronously or marked stale while it
  1669. * is on a delwri list we lazily remove it. To do this, the other party
  1670. * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
  1671. * It remains referenced and on the list. In a rare corner case it
  1672. * might get readded to a delwri list after the synchronous writeout, in
  1673. * which case we need just need to re-add the flag here.
  1674. */
  1675. bp->b_flags |= _XBF_DELWRI_Q;
  1676. if (list_empty(&bp->b_list)) {
  1677. atomic_inc(&bp->b_hold);
  1678. list_add_tail(&bp->b_list, list);
  1679. }
  1680. return true;
  1681. }
  1682. /*
  1683. * Compare function is more complex than it needs to be because
  1684. * the return value is only 32 bits and we are doing comparisons
  1685. * on 64 bit values
  1686. */
  1687. static int
  1688. xfs_buf_cmp(
  1689. void *priv,
  1690. struct list_head *a,
  1691. struct list_head *b)
  1692. {
  1693. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1694. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1695. xfs_daddr_t diff;
  1696. diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
  1697. if (diff < 0)
  1698. return -1;
  1699. if (diff > 0)
  1700. return 1;
  1701. return 0;
  1702. }
  1703. /*
  1704. * Submit buffers for write. If wait_list is specified, the buffers are
  1705. * submitted using sync I/O and placed on the wait list such that the caller can
  1706. * iowait each buffer. Otherwise async I/O is used and the buffers are released
  1707. * at I/O completion time. In either case, buffers remain locked until I/O
  1708. * completes and the buffer is released from the queue.
  1709. */
  1710. static int
  1711. xfs_buf_delwri_submit_buffers(
  1712. struct list_head *buffer_list,
  1713. struct list_head *wait_list)
  1714. {
  1715. struct xfs_buf *bp, *n;
  1716. LIST_HEAD (submit_list);
  1717. int pinned = 0;
  1718. struct blk_plug plug;
  1719. list_sort(NULL, buffer_list, xfs_buf_cmp);
  1720. blk_start_plug(&plug);
  1721. list_for_each_entry_safe(bp, n, buffer_list, b_list) {
  1722. if (!wait_list) {
  1723. if (xfs_buf_ispinned(bp)) {
  1724. pinned++;
  1725. continue;
  1726. }
  1727. if (!xfs_buf_trylock(bp))
  1728. continue;
  1729. } else {
  1730. xfs_buf_lock(bp);
  1731. }
  1732. /*
  1733. * Someone else might have written the buffer synchronously or
  1734. * marked it stale in the meantime. In that case only the
  1735. * _XBF_DELWRI_Q flag got cleared, and we have to drop the
  1736. * reference and remove it from the list here.
  1737. */
  1738. if (!(bp->b_flags & _XBF_DELWRI_Q)) {
  1739. list_del_init(&bp->b_list);
  1740. xfs_buf_relse(bp);
  1741. continue;
  1742. }
  1743. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1744. /*
  1745. * If we have a wait list, each buffer (and associated delwri
  1746. * queue reference) transfers to it and is submitted
  1747. * synchronously. Otherwise, drop the buffer from the delwri
  1748. * queue and submit async.
  1749. */
  1750. bp->b_flags &= ~_XBF_DELWRI_Q;
  1751. bp->b_flags |= XBF_WRITE;
  1752. if (wait_list) {
  1753. bp->b_flags &= ~XBF_ASYNC;
  1754. list_move_tail(&bp->b_list, wait_list);
  1755. } else {
  1756. bp->b_flags |= XBF_ASYNC;
  1757. list_del_init(&bp->b_list);
  1758. }
  1759. __xfs_buf_submit(bp, false);
  1760. }
  1761. blk_finish_plug(&plug);
  1762. return pinned;
  1763. }
  1764. /*
  1765. * Write out a buffer list asynchronously.
  1766. *
  1767. * This will take the @buffer_list, write all non-locked and non-pinned buffers
  1768. * out and not wait for I/O completion on any of the buffers. This interface
  1769. * is only safely useable for callers that can track I/O completion by higher
  1770. * level means, e.g. AIL pushing as the @buffer_list is consumed in this
  1771. * function.
  1772. *
  1773. * Note: this function will skip buffers it would block on, and in doing so
  1774. * leaves them on @buffer_list so they can be retried on a later pass. As such,
  1775. * it is up to the caller to ensure that the buffer list is fully submitted or
  1776. * cancelled appropriately when they are finished with the list. Failure to
  1777. * cancel or resubmit the list until it is empty will result in leaked buffers
  1778. * at unmount time.
  1779. */
  1780. int
  1781. xfs_buf_delwri_submit_nowait(
  1782. struct list_head *buffer_list)
  1783. {
  1784. return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
  1785. }
  1786. /*
  1787. * Write out a buffer list synchronously.
  1788. *
  1789. * This will take the @buffer_list, write all buffers out and wait for I/O
  1790. * completion on all of the buffers. @buffer_list is consumed by the function,
  1791. * so callers must have some other way of tracking buffers if they require such
  1792. * functionality.
  1793. */
  1794. int
  1795. xfs_buf_delwri_submit(
  1796. struct list_head *buffer_list)
  1797. {
  1798. LIST_HEAD (wait_list);
  1799. int error = 0, error2;
  1800. struct xfs_buf *bp;
  1801. xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
  1802. /* Wait for IO to complete. */
  1803. while (!list_empty(&wait_list)) {
  1804. bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
  1805. list_del_init(&bp->b_list);
  1806. /*
  1807. * Wait on the locked buffer, check for errors and unlock and
  1808. * release the delwri queue reference.
  1809. */
  1810. error2 = xfs_buf_iowait(bp);
  1811. xfs_buf_relse(bp);
  1812. if (!error)
  1813. error = error2;
  1814. }
  1815. return error;
  1816. }
  1817. /*
  1818. * Push a single buffer on a delwri queue.
  1819. *
  1820. * The purpose of this function is to submit a single buffer of a delwri queue
  1821. * and return with the buffer still on the original queue. The waiting delwri
  1822. * buffer submission infrastructure guarantees transfer of the delwri queue
  1823. * buffer reference to a temporary wait list. We reuse this infrastructure to
  1824. * transfer the buffer back to the original queue.
  1825. *
  1826. * Note the buffer transitions from the queued state, to the submitted and wait
  1827. * listed state and back to the queued state during this call. The buffer
  1828. * locking and queue management logic between _delwri_pushbuf() and
  1829. * _delwri_queue() guarantee that the buffer cannot be queued to another list
  1830. * before returning.
  1831. */
  1832. int
  1833. xfs_buf_delwri_pushbuf(
  1834. struct xfs_buf *bp,
  1835. struct list_head *buffer_list)
  1836. {
  1837. LIST_HEAD (submit_list);
  1838. int error;
  1839. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1840. trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
  1841. /*
  1842. * Isolate the buffer to a new local list so we can submit it for I/O
  1843. * independently from the rest of the original list.
  1844. */
  1845. xfs_buf_lock(bp);
  1846. list_move(&bp->b_list, &submit_list);
  1847. xfs_buf_unlock(bp);
  1848. /*
  1849. * Delwri submission clears the DELWRI_Q buffer flag and returns with
  1850. * the buffer on the wait list with the original reference. Rather than
  1851. * bounce the buffer from a local wait list back to the original list
  1852. * after I/O completion, reuse the original list as the wait list.
  1853. */
  1854. xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
  1855. /*
  1856. * The buffer is now locked, under I/O and wait listed on the original
  1857. * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
  1858. * return with the buffer unlocked and on the original queue.
  1859. */
  1860. error = xfs_buf_iowait(bp);
  1861. bp->b_flags |= _XBF_DELWRI_Q;
  1862. xfs_buf_unlock(bp);
  1863. return error;
  1864. }
  1865. int __init
  1866. xfs_buf_init(void)
  1867. {
  1868. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1869. KM_ZONE_HWALIGN, NULL);
  1870. if (!xfs_buf_zone)
  1871. goto out;
  1872. return 0;
  1873. out:
  1874. return -ENOMEM;
  1875. }
  1876. void
  1877. xfs_buf_terminate(void)
  1878. {
  1879. kmem_zone_destroy(xfs_buf_zone);
  1880. }
  1881. void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
  1882. {
  1883. /*
  1884. * Set the lru reference count to 0 based on the error injection tag.
  1885. * This allows userspace to disrupt buffer caching for debug/testing
  1886. * purposes.
  1887. */
  1888. if (XFS_TEST_ERROR(false, bp->b_target->bt_mount,
  1889. XFS_ERRTAG_BUF_LRU_REF))
  1890. lru_ref = 0;
  1891. atomic_set(&bp->b_lru_ref, lru_ref);
  1892. }