xfs_buf.c 43 KB

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