xfs_buf.c 44 KB

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