xfs_buf.c 45 KB

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