xfs_buf.c 52 KB

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