xfs_buf.c 41 KB

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