xfs_buf.c 51 KB

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