xfs_buf.c 48 KB

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