xfs_buf.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  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 <linux/list_sort.h>
  37. #include "xfs_sb.h"
  38. #include "xfs_inum.h"
  39. #include "xfs_log.h"
  40. #include "xfs_ag.h"
  41. #include "xfs_mount.h"
  42. #include "xfs_trace.h"
  43. static kmem_zone_t *xfs_buf_zone;
  44. STATIC int xfsbufd(void *);
  45. STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
  46. static struct workqueue_struct *xfslogd_workqueue;
  47. struct workqueue_struct *xfsdatad_workqueue;
  48. struct workqueue_struct *xfsconvertd_workqueue;
  49. #ifdef XFS_BUF_LOCK_TRACKING
  50. # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
  51. # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
  52. # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
  53. #else
  54. # define XB_SET_OWNER(bp) do { } while (0)
  55. # define XB_CLEAR_OWNER(bp) do { } while (0)
  56. # define XB_GET_OWNER(bp) do { } while (0)
  57. #endif
  58. #define xb_to_gfp(flags) \
  59. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
  60. ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
  61. #define xb_to_km(flags) \
  62. (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
  63. #define xfs_buf_allocate(flags) \
  64. kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
  65. #define xfs_buf_deallocate(bp) \
  66. kmem_zone_free(xfs_buf_zone, (bp));
  67. static inline int
  68. xfs_buf_is_vmapped(
  69. struct xfs_buf *bp)
  70. {
  71. /*
  72. * Return true if the buffer is vmapped.
  73. *
  74. * The XBF_MAPPED flag is set if the buffer should be mapped, but the
  75. * code is clever enough to know it doesn't have to map a single page,
  76. * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
  77. */
  78. return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
  79. }
  80. static inline int
  81. xfs_buf_vmap_len(
  82. struct xfs_buf *bp)
  83. {
  84. return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
  85. }
  86. /*
  87. * Page Region interfaces.
  88. *
  89. * For pages in filesystems where the blocksize is smaller than the
  90. * pagesize, we use the page->private field (long) to hold a bitmap
  91. * of uptodate regions within the page.
  92. *
  93. * Each such region is "bytes per page / bits per long" bytes long.
  94. *
  95. * NBPPR == number-of-bytes-per-page-region
  96. * BTOPR == bytes-to-page-region (rounded up)
  97. * BTOPRT == bytes-to-page-region-truncated (rounded down)
  98. */
  99. #if (BITS_PER_LONG == 32)
  100. #define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
  101. #elif (BITS_PER_LONG == 64)
  102. #define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
  103. #else
  104. #error BITS_PER_LONG must be 32 or 64
  105. #endif
  106. #define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
  107. #define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
  108. #define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
  109. STATIC unsigned long
  110. page_region_mask(
  111. size_t offset,
  112. size_t length)
  113. {
  114. unsigned long mask;
  115. int first, final;
  116. first = BTOPR(offset);
  117. final = BTOPRT(offset + length - 1);
  118. first = min(first, final);
  119. mask = ~0UL;
  120. mask <<= BITS_PER_LONG - (final - first);
  121. mask >>= BITS_PER_LONG - (final);
  122. ASSERT(offset + length <= PAGE_CACHE_SIZE);
  123. ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
  124. return mask;
  125. }
  126. STATIC void
  127. set_page_region(
  128. struct page *page,
  129. size_t offset,
  130. size_t length)
  131. {
  132. set_page_private(page,
  133. page_private(page) | page_region_mask(offset, length));
  134. if (page_private(page) == ~0UL)
  135. SetPageUptodate(page);
  136. }
  137. STATIC int
  138. test_page_region(
  139. struct page *page,
  140. size_t offset,
  141. size_t length)
  142. {
  143. unsigned long mask = page_region_mask(offset, length);
  144. return (mask && (page_private(page) & mask) == mask);
  145. }
  146. /*
  147. * xfs_buf_lru_add - add a buffer to the LRU.
  148. *
  149. * The LRU takes a new reference to the buffer so that it will only be freed
  150. * once the shrinker takes the buffer off the LRU.
  151. */
  152. STATIC void
  153. xfs_buf_lru_add(
  154. struct xfs_buf *bp)
  155. {
  156. struct xfs_buftarg *btp = bp->b_target;
  157. spin_lock(&btp->bt_lru_lock);
  158. if (list_empty(&bp->b_lru)) {
  159. atomic_inc(&bp->b_hold);
  160. list_add_tail(&bp->b_lru, &btp->bt_lru);
  161. btp->bt_lru_nr++;
  162. }
  163. spin_unlock(&btp->bt_lru_lock);
  164. }
  165. /*
  166. * xfs_buf_lru_del - remove a buffer from the LRU
  167. *
  168. * The unlocked check is safe here because it only occurs when there are not
  169. * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
  170. * to optimise the shrinker removing the buffer from the LRU and calling
  171. * xfs_buf_free(). i.e. it removes an unneccessary round trip on the
  172. * bt_lru_lock.
  173. */
  174. STATIC void
  175. xfs_buf_lru_del(
  176. struct xfs_buf *bp)
  177. {
  178. struct xfs_buftarg *btp = bp->b_target;
  179. if (list_empty(&bp->b_lru))
  180. return;
  181. spin_lock(&btp->bt_lru_lock);
  182. if (!list_empty(&bp->b_lru)) {
  183. list_del_init(&bp->b_lru);
  184. btp->bt_lru_nr--;
  185. }
  186. spin_unlock(&btp->bt_lru_lock);
  187. }
  188. /*
  189. * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  190. * b_lru_ref count so that the buffer is freed immediately when the buffer
  191. * reference count falls to zero. If the buffer is already on the LRU, we need
  192. * to remove the reference that LRU holds on the buffer.
  193. *
  194. * This prevents build-up of stale buffers on the LRU.
  195. */
  196. void
  197. xfs_buf_stale(
  198. struct xfs_buf *bp)
  199. {
  200. bp->b_flags |= XBF_STALE;
  201. atomic_set(&(bp)->b_lru_ref, 0);
  202. if (!list_empty(&bp->b_lru)) {
  203. struct xfs_buftarg *btp = bp->b_target;
  204. spin_lock(&btp->bt_lru_lock);
  205. if (!list_empty(&bp->b_lru)) {
  206. list_del_init(&bp->b_lru);
  207. btp->bt_lru_nr--;
  208. atomic_dec(&bp->b_hold);
  209. }
  210. spin_unlock(&btp->bt_lru_lock);
  211. }
  212. ASSERT(atomic_read(&bp->b_hold) >= 1);
  213. }
  214. STATIC void
  215. _xfs_buf_initialize(
  216. xfs_buf_t *bp,
  217. xfs_buftarg_t *target,
  218. xfs_off_t range_base,
  219. size_t range_length,
  220. xfs_buf_flags_t flags)
  221. {
  222. /*
  223. * We don't want certain flags to appear in b_flags.
  224. */
  225. flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
  226. memset(bp, 0, sizeof(xfs_buf_t));
  227. atomic_set(&bp->b_hold, 1);
  228. atomic_set(&bp->b_lru_ref, 1);
  229. init_completion(&bp->b_iowait);
  230. INIT_LIST_HEAD(&bp->b_lru);
  231. INIT_LIST_HEAD(&bp->b_list);
  232. RB_CLEAR_NODE(&bp->b_rbnode);
  233. sema_init(&bp->b_sema, 0); /* held, no waiters */
  234. XB_SET_OWNER(bp);
  235. bp->b_target = target;
  236. bp->b_file_offset = range_base;
  237. /*
  238. * Set buffer_length and count_desired to the same value initially.
  239. * I/O routines should use count_desired, which will be the same in
  240. * most cases but may be reset (e.g. XFS recovery).
  241. */
  242. bp->b_buffer_length = bp->b_count_desired = range_length;
  243. bp->b_flags = flags;
  244. bp->b_bn = XFS_BUF_DADDR_NULL;
  245. atomic_set(&bp->b_pin_count, 0);
  246. init_waitqueue_head(&bp->b_waiters);
  247. XFS_STATS_INC(xb_create);
  248. trace_xfs_buf_init(bp, _RET_IP_);
  249. }
  250. /*
  251. * Allocate a page array capable of holding a specified number
  252. * of pages, and point the page buf at it.
  253. */
  254. STATIC int
  255. _xfs_buf_get_pages(
  256. xfs_buf_t *bp,
  257. int page_count,
  258. xfs_buf_flags_t flags)
  259. {
  260. /* Make sure that we have a page list */
  261. if (bp->b_pages == NULL) {
  262. bp->b_offset = xfs_buf_poff(bp->b_file_offset);
  263. bp->b_page_count = page_count;
  264. if (page_count <= XB_PAGES) {
  265. bp->b_pages = bp->b_page_array;
  266. } else {
  267. bp->b_pages = kmem_alloc(sizeof(struct page *) *
  268. page_count, xb_to_km(flags));
  269. if (bp->b_pages == NULL)
  270. return -ENOMEM;
  271. }
  272. memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
  273. }
  274. return 0;
  275. }
  276. /*
  277. * Frees b_pages if it was allocated.
  278. */
  279. STATIC void
  280. _xfs_buf_free_pages(
  281. xfs_buf_t *bp)
  282. {
  283. if (bp->b_pages != bp->b_page_array) {
  284. kmem_free(bp->b_pages);
  285. bp->b_pages = NULL;
  286. }
  287. }
  288. /*
  289. * Releases the specified buffer.
  290. *
  291. * The modification state of any associated pages is left unchanged.
  292. * The buffer most not be on any hash - use xfs_buf_rele instead for
  293. * hashed and refcounted buffers
  294. */
  295. void
  296. xfs_buf_free(
  297. xfs_buf_t *bp)
  298. {
  299. trace_xfs_buf_free(bp, _RET_IP_);
  300. ASSERT(list_empty(&bp->b_lru));
  301. if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
  302. uint i;
  303. if (xfs_buf_is_vmapped(bp))
  304. vm_unmap_ram(bp->b_addr - bp->b_offset,
  305. bp->b_page_count);
  306. for (i = 0; i < bp->b_page_count; i++) {
  307. struct page *page = bp->b_pages[i];
  308. if (bp->b_flags & _XBF_PAGE_CACHE)
  309. ASSERT(!PagePrivate(page));
  310. page_cache_release(page);
  311. }
  312. }
  313. _xfs_buf_free_pages(bp);
  314. xfs_buf_deallocate(bp);
  315. }
  316. /*
  317. * Finds all pages for buffer in question and builds it's page list.
  318. */
  319. STATIC int
  320. _xfs_buf_lookup_pages(
  321. xfs_buf_t *bp,
  322. uint flags)
  323. {
  324. struct address_space *mapping = bp->b_target->bt_mapping;
  325. size_t blocksize = bp->b_target->bt_bsize;
  326. size_t size = bp->b_count_desired;
  327. size_t nbytes, offset;
  328. gfp_t gfp_mask = xb_to_gfp(flags);
  329. unsigned short page_count, i;
  330. pgoff_t first;
  331. xfs_off_t end;
  332. int error;
  333. end = bp->b_file_offset + bp->b_buffer_length;
  334. page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
  335. error = _xfs_buf_get_pages(bp, page_count, flags);
  336. if (unlikely(error))
  337. return error;
  338. bp->b_flags |= _XBF_PAGE_CACHE;
  339. offset = bp->b_offset;
  340. first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
  341. for (i = 0; i < bp->b_page_count; i++) {
  342. struct page *page;
  343. uint retries = 0;
  344. retry:
  345. page = find_or_create_page(mapping, first + i, gfp_mask);
  346. if (unlikely(page == NULL)) {
  347. if (flags & XBF_READ_AHEAD) {
  348. bp->b_page_count = i;
  349. for (i = 0; i < bp->b_page_count; i++)
  350. unlock_page(bp->b_pages[i]);
  351. return -ENOMEM;
  352. }
  353. /*
  354. * This could deadlock.
  355. *
  356. * But until all the XFS lowlevel code is revamped to
  357. * handle buffer allocation failures we can't do much.
  358. */
  359. if (!(++retries % 100))
  360. xfs_err(NULL,
  361. "possible memory allocation deadlock in %s (mode:0x%x)",
  362. __func__, gfp_mask);
  363. XFS_STATS_INC(xb_page_retries);
  364. congestion_wait(BLK_RW_ASYNC, HZ/50);
  365. goto retry;
  366. }
  367. XFS_STATS_INC(xb_page_found);
  368. nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
  369. size -= nbytes;
  370. ASSERT(!PagePrivate(page));
  371. if (!PageUptodate(page)) {
  372. page_count--;
  373. if (blocksize >= PAGE_CACHE_SIZE) {
  374. if (flags & XBF_READ)
  375. bp->b_flags |= _XBF_PAGE_LOCKED;
  376. } else if (!PagePrivate(page)) {
  377. if (test_page_region(page, offset, nbytes))
  378. page_count++;
  379. }
  380. }
  381. bp->b_pages[i] = page;
  382. offset = 0;
  383. }
  384. if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
  385. for (i = 0; i < bp->b_page_count; i++)
  386. unlock_page(bp->b_pages[i]);
  387. }
  388. if (page_count == bp->b_page_count)
  389. bp->b_flags |= XBF_DONE;
  390. return error;
  391. }
  392. /*
  393. * Map buffer into kernel address-space if nessecary.
  394. */
  395. STATIC int
  396. _xfs_buf_map_pages(
  397. xfs_buf_t *bp,
  398. uint flags)
  399. {
  400. /* A single page buffer is always mappable */
  401. if (bp->b_page_count == 1) {
  402. bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
  403. bp->b_flags |= XBF_MAPPED;
  404. } else if (flags & XBF_MAPPED) {
  405. bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
  406. -1, PAGE_KERNEL);
  407. if (unlikely(bp->b_addr == NULL))
  408. return -ENOMEM;
  409. bp->b_addr += bp->b_offset;
  410. bp->b_flags |= XBF_MAPPED;
  411. }
  412. return 0;
  413. }
  414. /*
  415. * Finding and Reading Buffers
  416. */
  417. /*
  418. * Look up, and creates if absent, a lockable buffer for
  419. * a given range of an inode. The buffer is returned
  420. * locked. If other overlapping buffers exist, they are
  421. * released before the new buffer is created and locked,
  422. * which may imply that this call will block until those buffers
  423. * are unlocked. No I/O is implied by this call.
  424. */
  425. xfs_buf_t *
  426. _xfs_buf_find(
  427. xfs_buftarg_t *btp, /* block device target */
  428. xfs_off_t ioff, /* starting offset of range */
  429. size_t isize, /* length of range */
  430. xfs_buf_flags_t flags,
  431. xfs_buf_t *new_bp)
  432. {
  433. xfs_off_t range_base;
  434. size_t range_length;
  435. struct xfs_perag *pag;
  436. struct rb_node **rbp;
  437. struct rb_node *parent;
  438. xfs_buf_t *bp;
  439. range_base = (ioff << BBSHIFT);
  440. range_length = (isize << BBSHIFT);
  441. /* Check for IOs smaller than the sector size / not sector aligned */
  442. ASSERT(!(range_length < (1 << btp->bt_sshift)));
  443. ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
  444. /* get tree root */
  445. pag = xfs_perag_get(btp->bt_mount,
  446. xfs_daddr_to_agno(btp->bt_mount, ioff));
  447. /* walk tree */
  448. spin_lock(&pag->pag_buf_lock);
  449. rbp = &pag->pag_buf_tree.rb_node;
  450. parent = NULL;
  451. bp = NULL;
  452. while (*rbp) {
  453. parent = *rbp;
  454. bp = rb_entry(parent, struct xfs_buf, b_rbnode);
  455. if (range_base < bp->b_file_offset)
  456. rbp = &(*rbp)->rb_left;
  457. else if (range_base > bp->b_file_offset)
  458. rbp = &(*rbp)->rb_right;
  459. else {
  460. /*
  461. * found a block offset match. If the range doesn't
  462. * match, the only way this is allowed is if the buffer
  463. * in the cache is stale and the transaction that made
  464. * it stale has not yet committed. i.e. we are
  465. * reallocating a busy extent. Skip this buffer and
  466. * continue searching to the right for an exact match.
  467. */
  468. if (bp->b_buffer_length != range_length) {
  469. ASSERT(bp->b_flags & XBF_STALE);
  470. rbp = &(*rbp)->rb_right;
  471. continue;
  472. }
  473. atomic_inc(&bp->b_hold);
  474. goto found;
  475. }
  476. }
  477. /* No match found */
  478. if (new_bp) {
  479. _xfs_buf_initialize(new_bp, btp, range_base,
  480. range_length, flags);
  481. rb_link_node(&new_bp->b_rbnode, parent, rbp);
  482. rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
  483. /* the buffer keeps the perag reference until it is freed */
  484. new_bp->b_pag = pag;
  485. spin_unlock(&pag->pag_buf_lock);
  486. } else {
  487. XFS_STATS_INC(xb_miss_locked);
  488. spin_unlock(&pag->pag_buf_lock);
  489. xfs_perag_put(pag);
  490. }
  491. return new_bp;
  492. found:
  493. spin_unlock(&pag->pag_buf_lock);
  494. xfs_perag_put(pag);
  495. if (xfs_buf_cond_lock(bp)) {
  496. /* failed, so wait for the lock if requested. */
  497. if (!(flags & XBF_TRYLOCK)) {
  498. xfs_buf_lock(bp);
  499. XFS_STATS_INC(xb_get_locked_waited);
  500. } else {
  501. xfs_buf_rele(bp);
  502. XFS_STATS_INC(xb_busy_locked);
  503. return NULL;
  504. }
  505. }
  506. if (bp->b_flags & XBF_STALE) {
  507. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  508. bp->b_flags &= XBF_MAPPED;
  509. }
  510. trace_xfs_buf_find(bp, flags, _RET_IP_);
  511. XFS_STATS_INC(xb_get_locked);
  512. return bp;
  513. }
  514. /*
  515. * Assembles a buffer covering the specified range.
  516. * Storage in memory for all portions of the buffer will be allocated,
  517. * although backing storage may not be.
  518. */
  519. xfs_buf_t *
  520. xfs_buf_get(
  521. xfs_buftarg_t *target,/* target for buffer */
  522. xfs_off_t ioff, /* starting offset of range */
  523. size_t isize, /* length of range */
  524. xfs_buf_flags_t flags)
  525. {
  526. xfs_buf_t *bp, *new_bp;
  527. int error = 0, i;
  528. new_bp = xfs_buf_allocate(flags);
  529. if (unlikely(!new_bp))
  530. return NULL;
  531. bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
  532. if (bp == new_bp) {
  533. error = _xfs_buf_lookup_pages(bp, flags);
  534. if (error)
  535. goto no_buffer;
  536. } else {
  537. xfs_buf_deallocate(new_bp);
  538. if (unlikely(bp == NULL))
  539. return NULL;
  540. }
  541. for (i = 0; i < bp->b_page_count; i++)
  542. mark_page_accessed(bp->b_pages[i]);
  543. if (!(bp->b_flags & XBF_MAPPED)) {
  544. error = _xfs_buf_map_pages(bp, flags);
  545. if (unlikely(error)) {
  546. xfs_warn(target->bt_mount,
  547. "%s: failed to map pages\n", __func__);
  548. goto no_buffer;
  549. }
  550. }
  551. XFS_STATS_INC(xb_get);
  552. /*
  553. * Always fill in the block number now, the mapped cases can do
  554. * their own overlay of this later.
  555. */
  556. bp->b_bn = ioff;
  557. bp->b_count_desired = bp->b_buffer_length;
  558. trace_xfs_buf_get(bp, flags, _RET_IP_);
  559. return bp;
  560. no_buffer:
  561. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  562. xfs_buf_unlock(bp);
  563. xfs_buf_rele(bp);
  564. return NULL;
  565. }
  566. STATIC int
  567. _xfs_buf_read(
  568. xfs_buf_t *bp,
  569. xfs_buf_flags_t flags)
  570. {
  571. int status;
  572. ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
  573. ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
  574. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
  575. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  576. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
  577. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  578. status = xfs_buf_iorequest(bp);
  579. if (status || XFS_BUF_ISERROR(bp) || (flags & XBF_ASYNC))
  580. return status;
  581. return xfs_buf_iowait(bp);
  582. }
  583. xfs_buf_t *
  584. xfs_buf_read(
  585. xfs_buftarg_t *target,
  586. xfs_off_t ioff,
  587. size_t isize,
  588. xfs_buf_flags_t flags)
  589. {
  590. xfs_buf_t *bp;
  591. flags |= XBF_READ;
  592. bp = xfs_buf_get(target, ioff, isize, flags);
  593. if (bp) {
  594. trace_xfs_buf_read(bp, flags, _RET_IP_);
  595. if (!XFS_BUF_ISDONE(bp)) {
  596. XFS_STATS_INC(xb_get_read);
  597. _xfs_buf_read(bp, flags);
  598. } else if (flags & XBF_ASYNC) {
  599. /*
  600. * Read ahead call which is already satisfied,
  601. * drop the buffer
  602. */
  603. goto no_buffer;
  604. } else {
  605. /* We do not want read in the flags */
  606. bp->b_flags &= ~XBF_READ;
  607. }
  608. }
  609. return bp;
  610. no_buffer:
  611. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  612. xfs_buf_unlock(bp);
  613. xfs_buf_rele(bp);
  614. return NULL;
  615. }
  616. /*
  617. * If we are not low on memory then do the readahead in a deadlock
  618. * safe manner.
  619. */
  620. void
  621. xfs_buf_readahead(
  622. xfs_buftarg_t *target,
  623. xfs_off_t ioff,
  624. size_t isize)
  625. {
  626. struct backing_dev_info *bdi;
  627. bdi = target->bt_mapping->backing_dev_info;
  628. if (bdi_read_congested(bdi))
  629. return;
  630. xfs_buf_read(target, ioff, isize,
  631. XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
  632. }
  633. /*
  634. * Read an uncached buffer from disk. Allocates and returns a locked
  635. * buffer containing the disk contents or nothing.
  636. */
  637. struct xfs_buf *
  638. xfs_buf_read_uncached(
  639. struct xfs_mount *mp,
  640. struct xfs_buftarg *target,
  641. xfs_daddr_t daddr,
  642. size_t length,
  643. int flags)
  644. {
  645. xfs_buf_t *bp;
  646. int error;
  647. bp = xfs_buf_get_uncached(target, length, flags);
  648. if (!bp)
  649. return NULL;
  650. /* set up the buffer for a read IO */
  651. xfs_buf_lock(bp);
  652. XFS_BUF_SET_ADDR(bp, daddr);
  653. XFS_BUF_READ(bp);
  654. XFS_BUF_BUSY(bp);
  655. xfsbdstrat(mp, bp);
  656. error = xfs_buf_iowait(bp);
  657. if (error || bp->b_error) {
  658. xfs_buf_relse(bp);
  659. return NULL;
  660. }
  661. return bp;
  662. }
  663. xfs_buf_t *
  664. xfs_buf_get_empty(
  665. size_t len,
  666. xfs_buftarg_t *target)
  667. {
  668. xfs_buf_t *bp;
  669. bp = xfs_buf_allocate(0);
  670. if (bp)
  671. _xfs_buf_initialize(bp, target, 0, len, 0);
  672. return bp;
  673. }
  674. static inline struct page *
  675. mem_to_page(
  676. void *addr)
  677. {
  678. if ((!is_vmalloc_addr(addr))) {
  679. return virt_to_page(addr);
  680. } else {
  681. return vmalloc_to_page(addr);
  682. }
  683. }
  684. int
  685. xfs_buf_associate_memory(
  686. xfs_buf_t *bp,
  687. void *mem,
  688. size_t len)
  689. {
  690. int rval;
  691. int i = 0;
  692. unsigned long pageaddr;
  693. unsigned long offset;
  694. size_t buflen;
  695. int page_count;
  696. pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
  697. offset = (unsigned long)mem - pageaddr;
  698. buflen = PAGE_CACHE_ALIGN(len + offset);
  699. page_count = buflen >> PAGE_CACHE_SHIFT;
  700. /* Free any previous set of page pointers */
  701. if (bp->b_pages)
  702. _xfs_buf_free_pages(bp);
  703. bp->b_pages = NULL;
  704. bp->b_addr = mem;
  705. rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
  706. if (rval)
  707. return rval;
  708. bp->b_offset = offset;
  709. for (i = 0; i < bp->b_page_count; i++) {
  710. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  711. pageaddr += PAGE_CACHE_SIZE;
  712. }
  713. bp->b_count_desired = len;
  714. bp->b_buffer_length = buflen;
  715. bp->b_flags |= XBF_MAPPED;
  716. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  717. return 0;
  718. }
  719. xfs_buf_t *
  720. xfs_buf_get_uncached(
  721. struct xfs_buftarg *target,
  722. size_t len,
  723. int flags)
  724. {
  725. unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
  726. int error, i;
  727. xfs_buf_t *bp;
  728. bp = xfs_buf_allocate(0);
  729. if (unlikely(bp == NULL))
  730. goto fail;
  731. _xfs_buf_initialize(bp, target, 0, len, 0);
  732. error = _xfs_buf_get_pages(bp, page_count, 0);
  733. if (error)
  734. goto fail_free_buf;
  735. for (i = 0; i < page_count; i++) {
  736. bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
  737. if (!bp->b_pages[i])
  738. goto fail_free_mem;
  739. }
  740. bp->b_flags |= _XBF_PAGES;
  741. error = _xfs_buf_map_pages(bp, XBF_MAPPED);
  742. if (unlikely(error)) {
  743. xfs_warn(target->bt_mount,
  744. "%s: failed to map pages\n", __func__);
  745. goto fail_free_mem;
  746. }
  747. xfs_buf_unlock(bp);
  748. trace_xfs_buf_get_uncached(bp, _RET_IP_);
  749. return bp;
  750. fail_free_mem:
  751. while (--i >= 0)
  752. __free_page(bp->b_pages[i]);
  753. _xfs_buf_free_pages(bp);
  754. fail_free_buf:
  755. xfs_buf_deallocate(bp);
  756. fail:
  757. return NULL;
  758. }
  759. /*
  760. * Increment reference count on buffer, to hold the buffer concurrently
  761. * with another thread which may release (free) the buffer asynchronously.
  762. * Must hold the buffer already to call this function.
  763. */
  764. void
  765. xfs_buf_hold(
  766. xfs_buf_t *bp)
  767. {
  768. trace_xfs_buf_hold(bp, _RET_IP_);
  769. atomic_inc(&bp->b_hold);
  770. }
  771. /*
  772. * Releases a hold on the specified buffer. If the
  773. * the hold count is 1, calls xfs_buf_free.
  774. */
  775. void
  776. xfs_buf_rele(
  777. xfs_buf_t *bp)
  778. {
  779. struct xfs_perag *pag = bp->b_pag;
  780. trace_xfs_buf_rele(bp, _RET_IP_);
  781. if (!pag) {
  782. ASSERT(list_empty(&bp->b_lru));
  783. ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
  784. if (atomic_dec_and_test(&bp->b_hold))
  785. xfs_buf_free(bp);
  786. return;
  787. }
  788. ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
  789. ASSERT(atomic_read(&bp->b_hold) > 0);
  790. if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
  791. if (!(bp->b_flags & XBF_STALE) &&
  792. atomic_read(&bp->b_lru_ref)) {
  793. xfs_buf_lru_add(bp);
  794. spin_unlock(&pag->pag_buf_lock);
  795. } else {
  796. xfs_buf_lru_del(bp);
  797. ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
  798. rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
  799. spin_unlock(&pag->pag_buf_lock);
  800. xfs_perag_put(pag);
  801. xfs_buf_free(bp);
  802. }
  803. }
  804. }
  805. /*
  806. * Mutual exclusion on buffers. Locking model:
  807. *
  808. * Buffers associated with inodes for which buffer locking
  809. * is not enabled are not protected by semaphores, and are
  810. * assumed to be exclusively owned by the caller. There is a
  811. * spinlock in the buffer, used by the caller when concurrent
  812. * access is possible.
  813. */
  814. /*
  815. * Locks a buffer object, if it is not already locked. Note that this in
  816. * no way locks the underlying pages, so it is only useful for
  817. * synchronizing concurrent use of buffer objects, not for synchronizing
  818. * independent access to the underlying pages.
  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_cond_lock(
  830. xfs_buf_t *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_cond_lock(bp, _RET_IP_);
  839. return locked ? 0 : -EBUSY;
  840. }
  841. int
  842. xfs_buf_lock_value(
  843. xfs_buf_t *bp)
  844. {
  845. return bp->b_sema.count;
  846. }
  847. /*
  848. * Locks a buffer object.
  849. * Note that this in no way locks the underlying pages, so it is only
  850. * useful for synchronizing concurrent use of buffer objects, not for
  851. * synchronizing independent access to the underlying pages.
  852. *
  853. * If we come across a stale, pinned, locked buffer, we know that we
  854. * are being asked to lock a buffer that has been reallocated. Because
  855. * it is pinned, we know that the log has not been pushed to disk and
  856. * hence it will still be locked. Rather than sleeping until someone
  857. * else pushes the log, push it ourselves before trying to get the lock.
  858. */
  859. void
  860. xfs_buf_lock(
  861. xfs_buf_t *bp)
  862. {
  863. trace_xfs_buf_lock(bp, _RET_IP_);
  864. if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  865. xfs_log_force(bp->b_target->bt_mount, 0);
  866. if (atomic_read(&bp->b_io_remaining))
  867. blk_run_address_space(bp->b_target->bt_mapping);
  868. down(&bp->b_sema);
  869. XB_SET_OWNER(bp);
  870. trace_xfs_buf_lock_done(bp, _RET_IP_);
  871. }
  872. /*
  873. * Releases the lock on the buffer object.
  874. * If the buffer is marked delwri but is not queued, do so before we
  875. * unlock the buffer as we need to set flags correctly. We also need to
  876. * take a reference for the delwri queue because the unlocker is going to
  877. * drop their's and they don't know we just queued it.
  878. */
  879. void
  880. xfs_buf_unlock(
  881. xfs_buf_t *bp)
  882. {
  883. if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
  884. atomic_inc(&bp->b_hold);
  885. bp->b_flags |= XBF_ASYNC;
  886. xfs_buf_delwri_queue(bp, 0);
  887. }
  888. XB_CLEAR_OWNER(bp);
  889. up(&bp->b_sema);
  890. trace_xfs_buf_unlock(bp, _RET_IP_);
  891. }
  892. STATIC void
  893. xfs_buf_wait_unpin(
  894. xfs_buf_t *bp)
  895. {
  896. DECLARE_WAITQUEUE (wait, current);
  897. if (atomic_read(&bp->b_pin_count) == 0)
  898. return;
  899. add_wait_queue(&bp->b_waiters, &wait);
  900. for (;;) {
  901. set_current_state(TASK_UNINTERRUPTIBLE);
  902. if (atomic_read(&bp->b_pin_count) == 0)
  903. break;
  904. if (atomic_read(&bp->b_io_remaining))
  905. blk_run_address_space(bp->b_target->bt_mapping);
  906. schedule();
  907. }
  908. remove_wait_queue(&bp->b_waiters, &wait);
  909. set_current_state(TASK_RUNNING);
  910. }
  911. /*
  912. * Buffer Utility Routines
  913. */
  914. STATIC void
  915. xfs_buf_iodone_work(
  916. struct work_struct *work)
  917. {
  918. xfs_buf_t *bp =
  919. container_of(work, xfs_buf_t, b_iodone_work);
  920. if (bp->b_iodone)
  921. (*(bp->b_iodone))(bp);
  922. else if (bp->b_flags & XBF_ASYNC)
  923. xfs_buf_relse(bp);
  924. }
  925. void
  926. xfs_buf_ioend(
  927. xfs_buf_t *bp,
  928. int schedule)
  929. {
  930. trace_xfs_buf_iodone(bp, _RET_IP_);
  931. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  932. if (bp->b_error == 0)
  933. bp->b_flags |= XBF_DONE;
  934. if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
  935. if (schedule) {
  936. INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
  937. queue_work(xfslogd_workqueue, &bp->b_iodone_work);
  938. } else {
  939. xfs_buf_iodone_work(&bp->b_iodone_work);
  940. }
  941. } else {
  942. complete(&bp->b_iowait);
  943. }
  944. }
  945. void
  946. xfs_buf_ioerror(
  947. xfs_buf_t *bp,
  948. int error)
  949. {
  950. ASSERT(error >= 0 && error <= 0xffff);
  951. bp->b_error = (unsigned short)error;
  952. trace_xfs_buf_ioerror(bp, error, _RET_IP_);
  953. }
  954. int
  955. xfs_bwrite(
  956. struct xfs_mount *mp,
  957. struct xfs_buf *bp)
  958. {
  959. int error;
  960. bp->b_flags |= XBF_WRITE;
  961. bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
  962. xfs_buf_delwri_dequeue(bp);
  963. xfs_bdstrat_cb(bp);
  964. error = xfs_buf_iowait(bp);
  965. if (error)
  966. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  967. xfs_buf_relse(bp);
  968. return error;
  969. }
  970. void
  971. xfs_bdwrite(
  972. void *mp,
  973. struct xfs_buf *bp)
  974. {
  975. trace_xfs_buf_bdwrite(bp, _RET_IP_);
  976. bp->b_flags &= ~XBF_READ;
  977. bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
  978. xfs_buf_delwri_queue(bp, 1);
  979. }
  980. /*
  981. * Called when we want to stop a buffer from getting written or read.
  982. * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
  983. * so that the proper iodone callbacks get called.
  984. */
  985. STATIC int
  986. xfs_bioerror(
  987. xfs_buf_t *bp)
  988. {
  989. #ifdef XFSERRORDEBUG
  990. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  991. #endif
  992. /*
  993. * No need to wait until the buffer is unpinned, we aren't flushing it.
  994. */
  995. XFS_BUF_ERROR(bp, EIO);
  996. /*
  997. * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
  998. */
  999. XFS_BUF_UNREAD(bp);
  1000. XFS_BUF_UNDELAYWRITE(bp);
  1001. XFS_BUF_UNDONE(bp);
  1002. XFS_BUF_STALE(bp);
  1003. xfs_buf_ioend(bp, 0);
  1004. return EIO;
  1005. }
  1006. /*
  1007. * Same as xfs_bioerror, except that we are releasing the buffer
  1008. * here ourselves, and avoiding the xfs_buf_ioend call.
  1009. * This is meant for userdata errors; metadata bufs come with
  1010. * iodone functions attached, so that we can track down errors.
  1011. */
  1012. STATIC int
  1013. xfs_bioerror_relse(
  1014. struct xfs_buf *bp)
  1015. {
  1016. int64_t fl = XFS_BUF_BFLAGS(bp);
  1017. /*
  1018. * No need to wait until the buffer is unpinned.
  1019. * We aren't flushing it.
  1020. *
  1021. * chunkhold expects B_DONE to be set, whether
  1022. * we actually finish the I/O or not. We don't want to
  1023. * change that interface.
  1024. */
  1025. XFS_BUF_UNREAD(bp);
  1026. XFS_BUF_UNDELAYWRITE(bp);
  1027. XFS_BUF_DONE(bp);
  1028. XFS_BUF_STALE(bp);
  1029. XFS_BUF_CLR_IODONE_FUNC(bp);
  1030. if (!(fl & XBF_ASYNC)) {
  1031. /*
  1032. * Mark b_error and B_ERROR _both_.
  1033. * Lot's of chunkcache code assumes that.
  1034. * There's no reason to mark error for
  1035. * ASYNC buffers.
  1036. */
  1037. XFS_BUF_ERROR(bp, EIO);
  1038. XFS_BUF_FINISH_IOWAIT(bp);
  1039. } else {
  1040. xfs_buf_relse(bp);
  1041. }
  1042. return EIO;
  1043. }
  1044. /*
  1045. * All xfs metadata buffers except log state machine buffers
  1046. * get this attached as their b_bdstrat callback function.
  1047. * This is so that we can catch a buffer
  1048. * after prematurely unpinning it to forcibly shutdown the filesystem.
  1049. */
  1050. int
  1051. xfs_bdstrat_cb(
  1052. struct xfs_buf *bp)
  1053. {
  1054. if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  1055. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  1056. /*
  1057. * Metadata write that didn't get logged but
  1058. * written delayed anyway. These aren't associated
  1059. * with a transaction, and can be ignored.
  1060. */
  1061. if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
  1062. return xfs_bioerror_relse(bp);
  1063. else
  1064. return xfs_bioerror(bp);
  1065. }
  1066. xfs_buf_iorequest(bp);
  1067. return 0;
  1068. }
  1069. /*
  1070. * Wrapper around bdstrat so that we can stop data from going to disk in case
  1071. * we are shutting down the filesystem. Typically user data goes thru this
  1072. * path; one of the exceptions is the superblock.
  1073. */
  1074. void
  1075. xfsbdstrat(
  1076. struct xfs_mount *mp,
  1077. struct xfs_buf *bp)
  1078. {
  1079. if (XFS_FORCED_SHUTDOWN(mp)) {
  1080. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  1081. xfs_bioerror_relse(bp);
  1082. return;
  1083. }
  1084. xfs_buf_iorequest(bp);
  1085. }
  1086. STATIC void
  1087. _xfs_buf_ioend(
  1088. xfs_buf_t *bp,
  1089. int schedule)
  1090. {
  1091. if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
  1092. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  1093. xfs_buf_ioend(bp, schedule);
  1094. }
  1095. }
  1096. STATIC void
  1097. xfs_buf_bio_end_io(
  1098. struct bio *bio,
  1099. int error)
  1100. {
  1101. xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
  1102. unsigned int blocksize = bp->b_target->bt_bsize;
  1103. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  1104. xfs_buf_ioerror(bp, -error);
  1105. if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
  1106. invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
  1107. do {
  1108. struct page *page = bvec->bv_page;
  1109. ASSERT(!PagePrivate(page));
  1110. if (unlikely(bp->b_error)) {
  1111. if (bp->b_flags & XBF_READ)
  1112. ClearPageUptodate(page);
  1113. } else if (blocksize >= PAGE_CACHE_SIZE) {
  1114. SetPageUptodate(page);
  1115. } else if (!PagePrivate(page) &&
  1116. (bp->b_flags & _XBF_PAGE_CACHE)) {
  1117. set_page_region(page, bvec->bv_offset, bvec->bv_len);
  1118. }
  1119. if (--bvec >= bio->bi_io_vec)
  1120. prefetchw(&bvec->bv_page->flags);
  1121. if (bp->b_flags & _XBF_PAGE_LOCKED)
  1122. unlock_page(page);
  1123. } while (bvec >= bio->bi_io_vec);
  1124. _xfs_buf_ioend(bp, 1);
  1125. bio_put(bio);
  1126. }
  1127. STATIC void
  1128. _xfs_buf_ioapply(
  1129. xfs_buf_t *bp)
  1130. {
  1131. int rw, map_i, total_nr_pages, nr_pages;
  1132. struct bio *bio;
  1133. int offset = bp->b_offset;
  1134. int size = bp->b_count_desired;
  1135. sector_t sector = bp->b_bn;
  1136. unsigned int blocksize = bp->b_target->bt_bsize;
  1137. total_nr_pages = bp->b_page_count;
  1138. map_i = 0;
  1139. if (bp->b_flags & XBF_ORDERED) {
  1140. ASSERT(!(bp->b_flags & XBF_READ));
  1141. rw = WRITE_FLUSH_FUA;
  1142. } else if (bp->b_flags & XBF_LOG_BUFFER) {
  1143. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1144. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1145. rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
  1146. } else if (bp->b_flags & _XBF_RUN_QUEUES) {
  1147. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1148. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1149. rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
  1150. } else {
  1151. rw = (bp->b_flags & XBF_WRITE) ? WRITE :
  1152. (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
  1153. }
  1154. /* Special code path for reading a sub page size buffer in --
  1155. * we populate up the whole page, and hence the other metadata
  1156. * in the same page. This optimization is only valid when the
  1157. * filesystem block size is not smaller than the page size.
  1158. */
  1159. if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
  1160. ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
  1161. (XBF_READ|_XBF_PAGE_LOCKED)) &&
  1162. (blocksize >= PAGE_CACHE_SIZE)) {
  1163. bio = bio_alloc(GFP_NOIO, 1);
  1164. bio->bi_bdev = bp->b_target->bt_bdev;
  1165. bio->bi_sector = sector - (offset >> BBSHIFT);
  1166. bio->bi_end_io = xfs_buf_bio_end_io;
  1167. bio->bi_private = bp;
  1168. bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
  1169. size = 0;
  1170. atomic_inc(&bp->b_io_remaining);
  1171. goto submit_io;
  1172. }
  1173. next_chunk:
  1174. atomic_inc(&bp->b_io_remaining);
  1175. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1176. if (nr_pages > total_nr_pages)
  1177. nr_pages = total_nr_pages;
  1178. bio = bio_alloc(GFP_NOIO, nr_pages);
  1179. bio->bi_bdev = bp->b_target->bt_bdev;
  1180. bio->bi_sector = sector;
  1181. bio->bi_end_io = xfs_buf_bio_end_io;
  1182. bio->bi_private = bp;
  1183. for (; size && nr_pages; nr_pages--, map_i++) {
  1184. int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
  1185. if (nbytes > size)
  1186. nbytes = size;
  1187. rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
  1188. if (rbytes < nbytes)
  1189. break;
  1190. offset = 0;
  1191. sector += nbytes >> BBSHIFT;
  1192. size -= nbytes;
  1193. total_nr_pages--;
  1194. }
  1195. submit_io:
  1196. if (likely(bio->bi_size)) {
  1197. if (xfs_buf_is_vmapped(bp)) {
  1198. flush_kernel_vmap_range(bp->b_addr,
  1199. xfs_buf_vmap_len(bp));
  1200. }
  1201. submit_bio(rw, bio);
  1202. if (size)
  1203. goto next_chunk;
  1204. } else {
  1205. /*
  1206. * if we get here, no pages were added to the bio. However,
  1207. * we can't just error out here - if the pages are locked then
  1208. * we have to unlock them otherwise we can hang on a later
  1209. * access to the page.
  1210. */
  1211. xfs_buf_ioerror(bp, EIO);
  1212. if (bp->b_flags & _XBF_PAGE_LOCKED) {
  1213. int i;
  1214. for (i = 0; i < bp->b_page_count; i++)
  1215. unlock_page(bp->b_pages[i]);
  1216. }
  1217. bio_put(bio);
  1218. }
  1219. }
  1220. int
  1221. xfs_buf_iorequest(
  1222. xfs_buf_t *bp)
  1223. {
  1224. trace_xfs_buf_iorequest(bp, _RET_IP_);
  1225. if (bp->b_flags & XBF_DELWRI) {
  1226. xfs_buf_delwri_queue(bp, 1);
  1227. return 0;
  1228. }
  1229. if (bp->b_flags & XBF_WRITE) {
  1230. xfs_buf_wait_unpin(bp);
  1231. }
  1232. xfs_buf_hold(bp);
  1233. /* Set the count to 1 initially, this will stop an I/O
  1234. * completion callout which happens before we have started
  1235. * all the I/O from calling xfs_buf_ioend too early.
  1236. */
  1237. atomic_set(&bp->b_io_remaining, 1);
  1238. _xfs_buf_ioapply(bp);
  1239. _xfs_buf_ioend(bp, 0);
  1240. xfs_buf_rele(bp);
  1241. return 0;
  1242. }
  1243. /*
  1244. * Waits for I/O to complete on the buffer supplied.
  1245. * It returns immediately if no I/O is pending.
  1246. * It returns the I/O error code, if any, or 0 if there was no error.
  1247. */
  1248. int
  1249. xfs_buf_iowait(
  1250. xfs_buf_t *bp)
  1251. {
  1252. trace_xfs_buf_iowait(bp, _RET_IP_);
  1253. if (atomic_read(&bp->b_io_remaining))
  1254. blk_run_address_space(bp->b_target->bt_mapping);
  1255. wait_for_completion(&bp->b_iowait);
  1256. trace_xfs_buf_iowait_done(bp, _RET_IP_);
  1257. return bp->b_error;
  1258. }
  1259. xfs_caddr_t
  1260. xfs_buf_offset(
  1261. xfs_buf_t *bp,
  1262. size_t offset)
  1263. {
  1264. struct page *page;
  1265. if (bp->b_flags & XBF_MAPPED)
  1266. return XFS_BUF_PTR(bp) + offset;
  1267. offset += bp->b_offset;
  1268. page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
  1269. return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
  1270. }
  1271. /*
  1272. * Move data into or out of a buffer.
  1273. */
  1274. void
  1275. xfs_buf_iomove(
  1276. xfs_buf_t *bp, /* buffer to process */
  1277. size_t boff, /* starting buffer offset */
  1278. size_t bsize, /* length to copy */
  1279. void *data, /* data address */
  1280. xfs_buf_rw_t mode) /* read/write/zero flag */
  1281. {
  1282. size_t bend, cpoff, csize;
  1283. struct page *page;
  1284. bend = boff + bsize;
  1285. while (boff < bend) {
  1286. page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
  1287. cpoff = xfs_buf_poff(boff + bp->b_offset);
  1288. csize = min_t(size_t,
  1289. PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
  1290. ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
  1291. switch (mode) {
  1292. case XBRW_ZERO:
  1293. memset(page_address(page) + cpoff, 0, csize);
  1294. break;
  1295. case XBRW_READ:
  1296. memcpy(data, page_address(page) + cpoff, csize);
  1297. break;
  1298. case XBRW_WRITE:
  1299. memcpy(page_address(page) + cpoff, data, csize);
  1300. }
  1301. boff += csize;
  1302. data += csize;
  1303. }
  1304. }
  1305. /*
  1306. * Handling of buffer targets (buftargs).
  1307. */
  1308. /*
  1309. * Wait for any bufs with callbacks that have been submitted but have not yet
  1310. * returned. These buffers will have an elevated hold count, so wait on those
  1311. * while freeing all the buffers only held by the LRU.
  1312. */
  1313. void
  1314. xfs_wait_buftarg(
  1315. struct xfs_buftarg *btp)
  1316. {
  1317. struct xfs_buf *bp;
  1318. restart:
  1319. spin_lock(&btp->bt_lru_lock);
  1320. while (!list_empty(&btp->bt_lru)) {
  1321. bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
  1322. if (atomic_read(&bp->b_hold) > 1) {
  1323. spin_unlock(&btp->bt_lru_lock);
  1324. delay(100);
  1325. goto restart;
  1326. }
  1327. /*
  1328. * clear the LRU reference count so the bufer doesn't get
  1329. * ignored in xfs_buf_rele().
  1330. */
  1331. atomic_set(&bp->b_lru_ref, 0);
  1332. spin_unlock(&btp->bt_lru_lock);
  1333. xfs_buf_rele(bp);
  1334. spin_lock(&btp->bt_lru_lock);
  1335. }
  1336. spin_unlock(&btp->bt_lru_lock);
  1337. }
  1338. int
  1339. xfs_buftarg_shrink(
  1340. struct shrinker *shrink,
  1341. int nr_to_scan,
  1342. gfp_t mask)
  1343. {
  1344. struct xfs_buftarg *btp = container_of(shrink,
  1345. struct xfs_buftarg, bt_shrinker);
  1346. struct xfs_buf *bp;
  1347. LIST_HEAD(dispose);
  1348. if (!nr_to_scan)
  1349. return btp->bt_lru_nr;
  1350. spin_lock(&btp->bt_lru_lock);
  1351. while (!list_empty(&btp->bt_lru)) {
  1352. if (nr_to_scan-- <= 0)
  1353. break;
  1354. bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
  1355. /*
  1356. * Decrement the b_lru_ref count unless the value is already
  1357. * zero. If the value is already zero, we need to reclaim the
  1358. * buffer, otherwise it gets another trip through the LRU.
  1359. */
  1360. if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
  1361. list_move_tail(&bp->b_lru, &btp->bt_lru);
  1362. continue;
  1363. }
  1364. /*
  1365. * remove the buffer from the LRU now to avoid needing another
  1366. * lock round trip inside xfs_buf_rele().
  1367. */
  1368. list_move(&bp->b_lru, &dispose);
  1369. btp->bt_lru_nr--;
  1370. }
  1371. spin_unlock(&btp->bt_lru_lock);
  1372. while (!list_empty(&dispose)) {
  1373. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1374. list_del_init(&bp->b_lru);
  1375. xfs_buf_rele(bp);
  1376. }
  1377. return btp->bt_lru_nr;
  1378. }
  1379. void
  1380. xfs_free_buftarg(
  1381. struct xfs_mount *mp,
  1382. struct xfs_buftarg *btp)
  1383. {
  1384. unregister_shrinker(&btp->bt_shrinker);
  1385. xfs_flush_buftarg(btp, 1);
  1386. if (mp->m_flags & XFS_MOUNT_BARRIER)
  1387. xfs_blkdev_issue_flush(btp);
  1388. iput(btp->bt_mapping->host);
  1389. kthread_stop(btp->bt_task);
  1390. kmem_free(btp);
  1391. }
  1392. STATIC int
  1393. xfs_setsize_buftarg_flags(
  1394. xfs_buftarg_t *btp,
  1395. unsigned int blocksize,
  1396. unsigned int sectorsize,
  1397. int verbose)
  1398. {
  1399. btp->bt_bsize = blocksize;
  1400. btp->bt_sshift = ffs(sectorsize) - 1;
  1401. btp->bt_smask = sectorsize - 1;
  1402. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1403. xfs_warn(btp->bt_mount,
  1404. "Cannot set_blocksize to %u on device %s\n",
  1405. sectorsize, XFS_BUFTARG_NAME(btp));
  1406. return EINVAL;
  1407. }
  1408. if (verbose &&
  1409. (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
  1410. printk(KERN_WARNING
  1411. "XFS: %u byte sectors in use on device %s. "
  1412. "This is suboptimal; %u or greater is ideal.\n",
  1413. sectorsize, XFS_BUFTARG_NAME(btp),
  1414. (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
  1415. }
  1416. return 0;
  1417. }
  1418. /*
  1419. * When allocating the initial buffer target we have not yet
  1420. * read in the superblock, so don't know what sized sectors
  1421. * are being used is at this early stage. Play safe.
  1422. */
  1423. STATIC int
  1424. xfs_setsize_buftarg_early(
  1425. xfs_buftarg_t *btp,
  1426. struct block_device *bdev)
  1427. {
  1428. return xfs_setsize_buftarg_flags(btp,
  1429. PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
  1430. }
  1431. int
  1432. xfs_setsize_buftarg(
  1433. xfs_buftarg_t *btp,
  1434. unsigned int blocksize,
  1435. unsigned int sectorsize)
  1436. {
  1437. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1438. }
  1439. STATIC int
  1440. xfs_mapping_buftarg(
  1441. xfs_buftarg_t *btp,
  1442. struct block_device *bdev)
  1443. {
  1444. struct backing_dev_info *bdi;
  1445. struct inode *inode;
  1446. struct address_space *mapping;
  1447. static const struct address_space_operations mapping_aops = {
  1448. .sync_page = block_sync_page,
  1449. .migratepage = fail_migrate_page,
  1450. };
  1451. inode = new_inode(bdev->bd_inode->i_sb);
  1452. if (!inode) {
  1453. printk(KERN_WARNING
  1454. "XFS: Cannot allocate mapping inode for device %s\n",
  1455. XFS_BUFTARG_NAME(btp));
  1456. return ENOMEM;
  1457. }
  1458. inode->i_ino = get_next_ino();
  1459. inode->i_mode = S_IFBLK;
  1460. inode->i_bdev = bdev;
  1461. inode->i_rdev = bdev->bd_dev;
  1462. bdi = blk_get_backing_dev_info(bdev);
  1463. if (!bdi)
  1464. bdi = &default_backing_dev_info;
  1465. mapping = &inode->i_data;
  1466. mapping->a_ops = &mapping_aops;
  1467. mapping->backing_dev_info = bdi;
  1468. mapping_set_gfp_mask(mapping, GFP_NOFS);
  1469. btp->bt_mapping = mapping;
  1470. return 0;
  1471. }
  1472. STATIC int
  1473. xfs_alloc_delwrite_queue(
  1474. xfs_buftarg_t *btp,
  1475. const char *fsname)
  1476. {
  1477. INIT_LIST_HEAD(&btp->bt_delwrite_queue);
  1478. spin_lock_init(&btp->bt_delwrite_lock);
  1479. btp->bt_flags = 0;
  1480. btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
  1481. if (IS_ERR(btp->bt_task))
  1482. return PTR_ERR(btp->bt_task);
  1483. return 0;
  1484. }
  1485. xfs_buftarg_t *
  1486. xfs_alloc_buftarg(
  1487. struct xfs_mount *mp,
  1488. struct block_device *bdev,
  1489. int external,
  1490. const char *fsname)
  1491. {
  1492. xfs_buftarg_t *btp;
  1493. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1494. btp->bt_mount = mp;
  1495. btp->bt_dev = bdev->bd_dev;
  1496. btp->bt_bdev = bdev;
  1497. INIT_LIST_HEAD(&btp->bt_lru);
  1498. spin_lock_init(&btp->bt_lru_lock);
  1499. if (xfs_setsize_buftarg_early(btp, bdev))
  1500. goto error;
  1501. if (xfs_mapping_buftarg(btp, bdev))
  1502. goto error;
  1503. if (xfs_alloc_delwrite_queue(btp, fsname))
  1504. goto error;
  1505. btp->bt_shrinker.shrink = xfs_buftarg_shrink;
  1506. btp->bt_shrinker.seeks = DEFAULT_SEEKS;
  1507. register_shrinker(&btp->bt_shrinker);
  1508. return btp;
  1509. error:
  1510. kmem_free(btp);
  1511. return NULL;
  1512. }
  1513. /*
  1514. * Delayed write buffer handling
  1515. */
  1516. STATIC void
  1517. xfs_buf_delwri_queue(
  1518. xfs_buf_t *bp,
  1519. int unlock)
  1520. {
  1521. struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
  1522. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1523. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1524. ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
  1525. spin_lock(dwlk);
  1526. /* If already in the queue, dequeue and place at tail */
  1527. if (!list_empty(&bp->b_list)) {
  1528. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1529. if (unlock)
  1530. atomic_dec(&bp->b_hold);
  1531. list_del(&bp->b_list);
  1532. }
  1533. if (list_empty(dwq)) {
  1534. /* start xfsbufd as it is about to have something to do */
  1535. wake_up_process(bp->b_target->bt_task);
  1536. }
  1537. bp->b_flags |= _XBF_DELWRI_Q;
  1538. list_add_tail(&bp->b_list, dwq);
  1539. bp->b_queuetime = jiffies;
  1540. spin_unlock(dwlk);
  1541. if (unlock)
  1542. xfs_buf_unlock(bp);
  1543. }
  1544. void
  1545. xfs_buf_delwri_dequeue(
  1546. xfs_buf_t *bp)
  1547. {
  1548. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1549. int dequeued = 0;
  1550. spin_lock(dwlk);
  1551. if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
  1552. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1553. list_del_init(&bp->b_list);
  1554. dequeued = 1;
  1555. }
  1556. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
  1557. spin_unlock(dwlk);
  1558. if (dequeued)
  1559. xfs_buf_rele(bp);
  1560. trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
  1561. }
  1562. /*
  1563. * If a delwri buffer needs to be pushed before it has aged out, then promote
  1564. * it to the head of the delwri queue so that it will be flushed on the next
  1565. * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
  1566. * than the age currently needed to flush the buffer. Hence the next time the
  1567. * xfsbufd sees it is guaranteed to be considered old enough to flush.
  1568. */
  1569. void
  1570. xfs_buf_delwri_promote(
  1571. struct xfs_buf *bp)
  1572. {
  1573. struct xfs_buftarg *btp = bp->b_target;
  1574. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
  1575. ASSERT(bp->b_flags & XBF_DELWRI);
  1576. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1577. /*
  1578. * Check the buffer age before locking the delayed write queue as we
  1579. * don't need to promote buffers that are already past the flush age.
  1580. */
  1581. if (bp->b_queuetime < jiffies - age)
  1582. return;
  1583. bp->b_queuetime = jiffies - age;
  1584. spin_lock(&btp->bt_delwrite_lock);
  1585. list_move(&bp->b_list, &btp->bt_delwrite_queue);
  1586. spin_unlock(&btp->bt_delwrite_lock);
  1587. }
  1588. STATIC void
  1589. xfs_buf_runall_queues(
  1590. struct workqueue_struct *queue)
  1591. {
  1592. flush_workqueue(queue);
  1593. }
  1594. /*
  1595. * Move as many buffers as specified to the supplied list
  1596. * idicating if we skipped any buffers to prevent deadlocks.
  1597. */
  1598. STATIC int
  1599. xfs_buf_delwri_split(
  1600. xfs_buftarg_t *target,
  1601. struct list_head *list,
  1602. unsigned long age)
  1603. {
  1604. xfs_buf_t *bp, *n;
  1605. struct list_head *dwq = &target->bt_delwrite_queue;
  1606. spinlock_t *dwlk = &target->bt_delwrite_lock;
  1607. int skipped = 0;
  1608. int force;
  1609. force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1610. INIT_LIST_HEAD(list);
  1611. spin_lock(dwlk);
  1612. list_for_each_entry_safe(bp, n, dwq, b_list) {
  1613. ASSERT(bp->b_flags & XBF_DELWRI);
  1614. if (!XFS_BUF_ISPINNED(bp) && !xfs_buf_cond_lock(bp)) {
  1615. if (!force &&
  1616. time_before(jiffies, bp->b_queuetime + age)) {
  1617. xfs_buf_unlock(bp);
  1618. break;
  1619. }
  1620. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
  1621. _XBF_RUN_QUEUES);
  1622. bp->b_flags |= XBF_WRITE;
  1623. list_move_tail(&bp->b_list, list);
  1624. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1625. } else
  1626. skipped++;
  1627. }
  1628. spin_unlock(dwlk);
  1629. return skipped;
  1630. }
  1631. /*
  1632. * Compare function is more complex than it needs to be because
  1633. * the return value is only 32 bits and we are doing comparisons
  1634. * on 64 bit values
  1635. */
  1636. static int
  1637. xfs_buf_cmp(
  1638. void *priv,
  1639. struct list_head *a,
  1640. struct list_head *b)
  1641. {
  1642. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1643. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1644. xfs_daddr_t diff;
  1645. diff = ap->b_bn - bp->b_bn;
  1646. if (diff < 0)
  1647. return -1;
  1648. if (diff > 0)
  1649. return 1;
  1650. return 0;
  1651. }
  1652. void
  1653. xfs_buf_delwri_sort(
  1654. xfs_buftarg_t *target,
  1655. struct list_head *list)
  1656. {
  1657. list_sort(NULL, list, xfs_buf_cmp);
  1658. }
  1659. STATIC int
  1660. xfsbufd(
  1661. void *data)
  1662. {
  1663. xfs_buftarg_t *target = (xfs_buftarg_t *)data;
  1664. current->flags |= PF_MEMALLOC;
  1665. set_freezable();
  1666. do {
  1667. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
  1668. long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
  1669. int count = 0;
  1670. struct list_head tmp;
  1671. if (unlikely(freezing(current))) {
  1672. set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1673. refrigerator();
  1674. } else {
  1675. clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1676. }
  1677. /* sleep for a long time if there is nothing to do. */
  1678. if (list_empty(&target->bt_delwrite_queue))
  1679. tout = MAX_SCHEDULE_TIMEOUT;
  1680. schedule_timeout_interruptible(tout);
  1681. xfs_buf_delwri_split(target, &tmp, age);
  1682. list_sort(NULL, &tmp, xfs_buf_cmp);
  1683. while (!list_empty(&tmp)) {
  1684. struct xfs_buf *bp;
  1685. bp = list_first_entry(&tmp, struct xfs_buf, b_list);
  1686. list_del_init(&bp->b_list);
  1687. xfs_bdstrat_cb(bp);
  1688. count++;
  1689. }
  1690. if (count)
  1691. blk_run_address_space(target->bt_mapping);
  1692. } while (!kthread_should_stop());
  1693. return 0;
  1694. }
  1695. /*
  1696. * Go through all incore buffers, and release buffers if they belong to
  1697. * the given device. This is used in filesystem error handling to
  1698. * preserve the consistency of its metadata.
  1699. */
  1700. int
  1701. xfs_flush_buftarg(
  1702. xfs_buftarg_t *target,
  1703. int wait)
  1704. {
  1705. xfs_buf_t *bp;
  1706. int pincount = 0;
  1707. LIST_HEAD(tmp_list);
  1708. LIST_HEAD(wait_list);
  1709. xfs_buf_runall_queues(xfsconvertd_workqueue);
  1710. xfs_buf_runall_queues(xfsdatad_workqueue);
  1711. xfs_buf_runall_queues(xfslogd_workqueue);
  1712. set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1713. pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
  1714. /*
  1715. * Dropped the delayed write list lock, now walk the temporary list.
  1716. * All I/O is issued async and then if we need to wait for completion
  1717. * we do that after issuing all the IO.
  1718. */
  1719. list_sort(NULL, &tmp_list, xfs_buf_cmp);
  1720. while (!list_empty(&tmp_list)) {
  1721. bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
  1722. ASSERT(target == bp->b_target);
  1723. list_del_init(&bp->b_list);
  1724. if (wait) {
  1725. bp->b_flags &= ~XBF_ASYNC;
  1726. list_add(&bp->b_list, &wait_list);
  1727. }
  1728. xfs_bdstrat_cb(bp);
  1729. }
  1730. if (wait) {
  1731. /* Expedite and wait for IO to complete. */
  1732. blk_run_address_space(target->bt_mapping);
  1733. while (!list_empty(&wait_list)) {
  1734. bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
  1735. list_del_init(&bp->b_list);
  1736. xfs_buf_iowait(bp);
  1737. xfs_buf_relse(bp);
  1738. }
  1739. }
  1740. return pincount;
  1741. }
  1742. int __init
  1743. xfs_buf_init(void)
  1744. {
  1745. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1746. KM_ZONE_HWALIGN, NULL);
  1747. if (!xfs_buf_zone)
  1748. goto out;
  1749. xfslogd_workqueue = alloc_workqueue("xfslogd",
  1750. WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
  1751. if (!xfslogd_workqueue)
  1752. goto out_free_buf_zone;
  1753. xfsdatad_workqueue = alloc_workqueue("xfsdatad", WQ_MEM_RECLAIM, 1);
  1754. if (!xfsdatad_workqueue)
  1755. goto out_destroy_xfslogd_workqueue;
  1756. xfsconvertd_workqueue = alloc_workqueue("xfsconvertd",
  1757. WQ_MEM_RECLAIM, 1);
  1758. if (!xfsconvertd_workqueue)
  1759. goto out_destroy_xfsdatad_workqueue;
  1760. return 0;
  1761. out_destroy_xfsdatad_workqueue:
  1762. destroy_workqueue(xfsdatad_workqueue);
  1763. out_destroy_xfslogd_workqueue:
  1764. destroy_workqueue(xfslogd_workqueue);
  1765. out_free_buf_zone:
  1766. kmem_zone_destroy(xfs_buf_zone);
  1767. out:
  1768. return -ENOMEM;
  1769. }
  1770. void
  1771. xfs_buf_terminate(void)
  1772. {
  1773. destroy_workqueue(xfsconvertd_workqueue);
  1774. destroy_workqueue(xfsdatad_workqueue);
  1775. destroy_workqueue(xfslogd_workqueue);
  1776. kmem_zone_destroy(xfs_buf_zone);
  1777. }
  1778. #ifdef CONFIG_KDB_MODULES
  1779. struct list_head *
  1780. xfs_get_buftarg_list(void)
  1781. {
  1782. return &xfs_buftarg_list;
  1783. }
  1784. #endif