xfs_buf.c 39 KB

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