xfs_buf.c 45 KB

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