xfs_buf.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  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. atomic_inc(&bp->b_hold);
  389. goto found;
  390. }
  391. }
  392. /* No match found */
  393. if (new_bp) {
  394. _xfs_buf_initialize(new_bp, btp, range_base,
  395. range_length, flags);
  396. new_bp->b_hash = hash;
  397. list_add(&new_bp->b_hash_list, &hash->bh_list);
  398. } else {
  399. XFS_STATS_INC(xb_miss_locked);
  400. }
  401. spin_unlock(&hash->bh_lock);
  402. return new_bp;
  403. found:
  404. spin_unlock(&hash->bh_lock);
  405. /* Attempt to get the semaphore without sleeping,
  406. * if this does not work then we need to drop the
  407. * spinlock and do a hard attempt on the semaphore.
  408. */
  409. if (down_trylock(&bp->b_sema)) {
  410. if (!(flags & XBF_TRYLOCK)) {
  411. /* wait for buffer ownership */
  412. xfs_buf_lock(bp);
  413. XFS_STATS_INC(xb_get_locked_waited);
  414. } else {
  415. /* We asked for a trylock and failed, no need
  416. * to look at file offset and length here, we
  417. * know that this buffer at least overlaps our
  418. * buffer and is locked, therefore our buffer
  419. * either does not exist, or is this buffer.
  420. */
  421. xfs_buf_rele(bp);
  422. XFS_STATS_INC(xb_busy_locked);
  423. return NULL;
  424. }
  425. } else {
  426. /* trylock worked */
  427. XB_SET_OWNER(bp);
  428. }
  429. if (bp->b_flags & XBF_STALE) {
  430. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  431. bp->b_flags &= XBF_MAPPED;
  432. }
  433. trace_xfs_buf_find(bp, flags, _RET_IP_);
  434. XFS_STATS_INC(xb_get_locked);
  435. return bp;
  436. }
  437. /*
  438. * Assembles a buffer covering the specified range.
  439. * Storage in memory for all portions of the buffer will be allocated,
  440. * although backing storage may not be.
  441. */
  442. xfs_buf_t *
  443. xfs_buf_get(
  444. xfs_buftarg_t *target,/* target for buffer */
  445. xfs_off_t ioff, /* starting offset of range */
  446. size_t isize, /* length of range */
  447. xfs_buf_flags_t flags)
  448. {
  449. xfs_buf_t *bp, *new_bp;
  450. int error = 0, i;
  451. new_bp = xfs_buf_allocate(flags);
  452. if (unlikely(!new_bp))
  453. return NULL;
  454. bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
  455. if (bp == new_bp) {
  456. error = _xfs_buf_lookup_pages(bp, flags);
  457. if (error)
  458. goto no_buffer;
  459. } else {
  460. xfs_buf_deallocate(new_bp);
  461. if (unlikely(bp == NULL))
  462. return NULL;
  463. }
  464. for (i = 0; i < bp->b_page_count; i++)
  465. mark_page_accessed(bp->b_pages[i]);
  466. if (!(bp->b_flags & XBF_MAPPED)) {
  467. error = _xfs_buf_map_pages(bp, flags);
  468. if (unlikely(error)) {
  469. printk(KERN_WARNING "%s: failed to map pages\n",
  470. __func__);
  471. goto no_buffer;
  472. }
  473. }
  474. XFS_STATS_INC(xb_get);
  475. /*
  476. * Always fill in the block number now, the mapped cases can do
  477. * their own overlay of this later.
  478. */
  479. bp->b_bn = ioff;
  480. bp->b_count_desired = bp->b_buffer_length;
  481. trace_xfs_buf_get(bp, flags, _RET_IP_);
  482. return bp;
  483. no_buffer:
  484. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  485. xfs_buf_unlock(bp);
  486. xfs_buf_rele(bp);
  487. return NULL;
  488. }
  489. STATIC int
  490. _xfs_buf_read(
  491. xfs_buf_t *bp,
  492. xfs_buf_flags_t flags)
  493. {
  494. int status;
  495. ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
  496. ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
  497. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
  498. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  499. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
  500. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  501. status = xfs_buf_iorequest(bp);
  502. if (status || XFS_BUF_ISERROR(bp) || (flags & XBF_ASYNC))
  503. return status;
  504. return xfs_buf_iowait(bp);
  505. }
  506. xfs_buf_t *
  507. xfs_buf_read(
  508. xfs_buftarg_t *target,
  509. xfs_off_t ioff,
  510. size_t isize,
  511. xfs_buf_flags_t flags)
  512. {
  513. xfs_buf_t *bp;
  514. flags |= XBF_READ;
  515. bp = xfs_buf_get(target, ioff, isize, flags);
  516. if (bp) {
  517. trace_xfs_buf_read(bp, flags, _RET_IP_);
  518. if (!XFS_BUF_ISDONE(bp)) {
  519. XFS_STATS_INC(xb_get_read);
  520. _xfs_buf_read(bp, flags);
  521. } else if (flags & XBF_ASYNC) {
  522. /*
  523. * Read ahead call which is already satisfied,
  524. * drop the buffer
  525. */
  526. goto no_buffer;
  527. } else {
  528. /* We do not want read in the flags */
  529. bp->b_flags &= ~XBF_READ;
  530. }
  531. }
  532. return bp;
  533. no_buffer:
  534. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  535. xfs_buf_unlock(bp);
  536. xfs_buf_rele(bp);
  537. return NULL;
  538. }
  539. /*
  540. * If we are not low on memory then do the readahead in a deadlock
  541. * safe manner.
  542. */
  543. void
  544. xfs_buf_readahead(
  545. xfs_buftarg_t *target,
  546. xfs_off_t ioff,
  547. size_t isize,
  548. xfs_buf_flags_t flags)
  549. {
  550. struct backing_dev_info *bdi;
  551. bdi = target->bt_mapping->backing_dev_info;
  552. if (bdi_read_congested(bdi))
  553. return;
  554. flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
  555. xfs_buf_read(target, ioff, isize, flags);
  556. }
  557. /*
  558. * Read an uncached buffer from disk. Allocates and returns a locked
  559. * buffer containing the disk contents or nothing.
  560. */
  561. struct xfs_buf *
  562. xfs_buf_read_uncached(
  563. struct xfs_mount *mp,
  564. struct xfs_buftarg *target,
  565. xfs_daddr_t daddr,
  566. size_t length,
  567. int flags)
  568. {
  569. xfs_buf_t *bp;
  570. int error;
  571. bp = xfs_buf_get_uncached(target, length, flags);
  572. if (!bp)
  573. return NULL;
  574. /* set up the buffer for a read IO */
  575. xfs_buf_lock(bp);
  576. XFS_BUF_SET_ADDR(bp, daddr);
  577. XFS_BUF_READ(bp);
  578. XFS_BUF_BUSY(bp);
  579. xfsbdstrat(mp, bp);
  580. error = xfs_iowait(bp);
  581. if (error || bp->b_error) {
  582. xfs_buf_relse(bp);
  583. return NULL;
  584. }
  585. return bp;
  586. }
  587. xfs_buf_t *
  588. xfs_buf_get_empty(
  589. size_t len,
  590. xfs_buftarg_t *target)
  591. {
  592. xfs_buf_t *bp;
  593. bp = xfs_buf_allocate(0);
  594. if (bp)
  595. _xfs_buf_initialize(bp, target, 0, len, 0);
  596. return bp;
  597. }
  598. static inline struct page *
  599. mem_to_page(
  600. void *addr)
  601. {
  602. if ((!is_vmalloc_addr(addr))) {
  603. return virt_to_page(addr);
  604. } else {
  605. return vmalloc_to_page(addr);
  606. }
  607. }
  608. int
  609. xfs_buf_associate_memory(
  610. xfs_buf_t *bp,
  611. void *mem,
  612. size_t len)
  613. {
  614. int rval;
  615. int i = 0;
  616. unsigned long pageaddr;
  617. unsigned long offset;
  618. size_t buflen;
  619. int page_count;
  620. pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
  621. offset = (unsigned long)mem - pageaddr;
  622. buflen = PAGE_CACHE_ALIGN(len + offset);
  623. page_count = buflen >> PAGE_CACHE_SHIFT;
  624. /* Free any previous set of page pointers */
  625. if (bp->b_pages)
  626. _xfs_buf_free_pages(bp);
  627. bp->b_pages = NULL;
  628. bp->b_addr = mem;
  629. rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
  630. if (rval)
  631. return rval;
  632. bp->b_offset = offset;
  633. for (i = 0; i < bp->b_page_count; i++) {
  634. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  635. pageaddr += PAGE_CACHE_SIZE;
  636. }
  637. bp->b_count_desired = len;
  638. bp->b_buffer_length = buflen;
  639. bp->b_flags |= XBF_MAPPED;
  640. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  641. return 0;
  642. }
  643. xfs_buf_t *
  644. xfs_buf_get_uncached(
  645. struct xfs_buftarg *target,
  646. size_t len,
  647. int flags)
  648. {
  649. unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
  650. int error, i;
  651. xfs_buf_t *bp;
  652. bp = xfs_buf_allocate(0);
  653. if (unlikely(bp == NULL))
  654. goto fail;
  655. _xfs_buf_initialize(bp, target, 0, len, 0);
  656. error = _xfs_buf_get_pages(bp, page_count, 0);
  657. if (error)
  658. goto fail_free_buf;
  659. for (i = 0; i < page_count; i++) {
  660. bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
  661. if (!bp->b_pages[i])
  662. goto fail_free_mem;
  663. }
  664. bp->b_flags |= _XBF_PAGES;
  665. error = _xfs_buf_map_pages(bp, XBF_MAPPED);
  666. if (unlikely(error)) {
  667. printk(KERN_WARNING "%s: failed to map pages\n",
  668. __func__);
  669. goto fail_free_mem;
  670. }
  671. xfs_buf_unlock(bp);
  672. trace_xfs_buf_get_uncached(bp, _RET_IP_);
  673. return bp;
  674. fail_free_mem:
  675. while (--i >= 0)
  676. __free_page(bp->b_pages[i]);
  677. _xfs_buf_free_pages(bp);
  678. fail_free_buf:
  679. xfs_buf_deallocate(bp);
  680. fail:
  681. return NULL;
  682. }
  683. /*
  684. * Increment reference count on buffer, to hold the buffer concurrently
  685. * with another thread which may release (free) the buffer asynchronously.
  686. * Must hold the buffer already to call this function.
  687. */
  688. void
  689. xfs_buf_hold(
  690. xfs_buf_t *bp)
  691. {
  692. trace_xfs_buf_hold(bp, _RET_IP_);
  693. atomic_inc(&bp->b_hold);
  694. }
  695. /*
  696. * Releases a hold on the specified buffer. If the
  697. * the hold count is 1, calls xfs_buf_free.
  698. */
  699. void
  700. xfs_buf_rele(
  701. xfs_buf_t *bp)
  702. {
  703. xfs_bufhash_t *hash = bp->b_hash;
  704. trace_xfs_buf_rele(bp, _RET_IP_);
  705. if (unlikely(!hash)) {
  706. ASSERT(!bp->b_relse);
  707. if (atomic_dec_and_test(&bp->b_hold))
  708. xfs_buf_free(bp);
  709. return;
  710. }
  711. ASSERT(atomic_read(&bp->b_hold) > 0);
  712. if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
  713. if (bp->b_relse) {
  714. atomic_inc(&bp->b_hold);
  715. spin_unlock(&hash->bh_lock);
  716. (*(bp->b_relse)) (bp);
  717. } else if (bp->b_flags & XBF_FS_MANAGED) {
  718. spin_unlock(&hash->bh_lock);
  719. } else {
  720. ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
  721. list_del_init(&bp->b_hash_list);
  722. spin_unlock(&hash->bh_lock);
  723. xfs_buf_free(bp);
  724. }
  725. }
  726. }
  727. /*
  728. * Mutual exclusion on buffers. Locking model:
  729. *
  730. * Buffers associated with inodes for which buffer locking
  731. * is not enabled are not protected by semaphores, and are
  732. * assumed to be exclusively owned by the caller. There is a
  733. * spinlock in the buffer, used by the caller when concurrent
  734. * access is possible.
  735. */
  736. /*
  737. * Locks a buffer object, if it is not already locked.
  738. * Note that this in no way locks the underlying pages, so it is only
  739. * useful for synchronizing concurrent use of buffer objects, not for
  740. * synchronizing independent access to the underlying pages.
  741. */
  742. int
  743. xfs_buf_cond_lock(
  744. xfs_buf_t *bp)
  745. {
  746. int locked;
  747. locked = down_trylock(&bp->b_sema) == 0;
  748. if (locked)
  749. XB_SET_OWNER(bp);
  750. trace_xfs_buf_cond_lock(bp, _RET_IP_);
  751. return locked ? 0 : -EBUSY;
  752. }
  753. int
  754. xfs_buf_lock_value(
  755. xfs_buf_t *bp)
  756. {
  757. return bp->b_sema.count;
  758. }
  759. /*
  760. * Locks a buffer object.
  761. * Note that this in no way locks the underlying pages, so it is only
  762. * useful for synchronizing concurrent use of buffer objects, not for
  763. * synchronizing independent access to the underlying pages.
  764. *
  765. * If we come across a stale, pinned, locked buffer, we know that we
  766. * are being asked to lock a buffer that has been reallocated. Because
  767. * it is pinned, we know that the log has not been pushed to disk and
  768. * hence it will still be locked. Rather than sleeping until someone
  769. * else pushes the log, push it ourselves before trying to get the lock.
  770. */
  771. void
  772. xfs_buf_lock(
  773. xfs_buf_t *bp)
  774. {
  775. trace_xfs_buf_lock(bp, _RET_IP_);
  776. if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  777. xfs_log_force(bp->b_mount, 0);
  778. if (atomic_read(&bp->b_io_remaining))
  779. blk_run_address_space(bp->b_target->bt_mapping);
  780. down(&bp->b_sema);
  781. XB_SET_OWNER(bp);
  782. trace_xfs_buf_lock_done(bp, _RET_IP_);
  783. }
  784. /*
  785. * Releases the lock on the buffer object.
  786. * If the buffer is marked delwri but is not queued, do so before we
  787. * unlock the buffer as we need to set flags correctly. We also need to
  788. * take a reference for the delwri queue because the unlocker is going to
  789. * drop their's and they don't know we just queued it.
  790. */
  791. void
  792. xfs_buf_unlock(
  793. xfs_buf_t *bp)
  794. {
  795. if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
  796. atomic_inc(&bp->b_hold);
  797. bp->b_flags |= XBF_ASYNC;
  798. xfs_buf_delwri_queue(bp, 0);
  799. }
  800. XB_CLEAR_OWNER(bp);
  801. up(&bp->b_sema);
  802. trace_xfs_buf_unlock(bp, _RET_IP_);
  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_mount = mp;
  885. bp->b_flags |= XBF_WRITE;
  886. bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
  887. xfs_buf_delwri_dequeue(bp);
  888. xfs_bdstrat_cb(bp);
  889. error = xfs_buf_iowait(bp);
  890. if (error)
  891. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  892. xfs_buf_relse(bp);
  893. return error;
  894. }
  895. void
  896. xfs_bdwrite(
  897. void *mp,
  898. struct xfs_buf *bp)
  899. {
  900. trace_xfs_buf_bdwrite(bp, _RET_IP_);
  901. bp->b_mount = mp;
  902. bp->b_flags &= ~XBF_READ;
  903. bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
  904. xfs_buf_delwri_queue(bp, 1);
  905. }
  906. /*
  907. * Called when we want to stop a buffer from getting written or read.
  908. * We attach the EIO error, muck with its flags, and call biodone
  909. * so that the proper iodone callbacks get called.
  910. */
  911. STATIC int
  912. xfs_bioerror(
  913. xfs_buf_t *bp)
  914. {
  915. #ifdef XFSERRORDEBUG
  916. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  917. #endif
  918. /*
  919. * No need to wait until the buffer is unpinned, we aren't flushing it.
  920. */
  921. XFS_BUF_ERROR(bp, EIO);
  922. /*
  923. * We're calling biodone, so delete XBF_DONE flag.
  924. */
  925. XFS_BUF_UNREAD(bp);
  926. XFS_BUF_UNDELAYWRITE(bp);
  927. XFS_BUF_UNDONE(bp);
  928. XFS_BUF_STALE(bp);
  929. xfs_biodone(bp);
  930. return EIO;
  931. }
  932. /*
  933. * Same as xfs_bioerror, except that we are releasing the buffer
  934. * here ourselves, and avoiding the biodone call.
  935. * This is meant for userdata errors; metadata bufs come with
  936. * iodone functions attached, so that we can track down errors.
  937. */
  938. STATIC int
  939. xfs_bioerror_relse(
  940. struct xfs_buf *bp)
  941. {
  942. int64_t fl = XFS_BUF_BFLAGS(bp);
  943. /*
  944. * No need to wait until the buffer is unpinned.
  945. * We aren't flushing it.
  946. *
  947. * chunkhold expects B_DONE to be set, whether
  948. * we actually finish the I/O or not. We don't want to
  949. * change that interface.
  950. */
  951. XFS_BUF_UNREAD(bp);
  952. XFS_BUF_UNDELAYWRITE(bp);
  953. XFS_BUF_DONE(bp);
  954. XFS_BUF_STALE(bp);
  955. XFS_BUF_CLR_IODONE_FUNC(bp);
  956. if (!(fl & XBF_ASYNC)) {
  957. /*
  958. * Mark b_error and B_ERROR _both_.
  959. * Lot's of chunkcache code assumes that.
  960. * There's no reason to mark error for
  961. * ASYNC buffers.
  962. */
  963. XFS_BUF_ERROR(bp, EIO);
  964. XFS_BUF_FINISH_IOWAIT(bp);
  965. } else {
  966. xfs_buf_relse(bp);
  967. }
  968. return EIO;
  969. }
  970. /*
  971. * All xfs metadata buffers except log state machine buffers
  972. * get this attached as their b_bdstrat callback function.
  973. * This is so that we can catch a buffer
  974. * after prematurely unpinning it to forcibly shutdown the filesystem.
  975. */
  976. int
  977. xfs_bdstrat_cb(
  978. struct xfs_buf *bp)
  979. {
  980. if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
  981. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  982. /*
  983. * Metadata write that didn't get logged but
  984. * written delayed anyway. These aren't associated
  985. * with a transaction, and can be ignored.
  986. */
  987. if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
  988. return xfs_bioerror_relse(bp);
  989. else
  990. return xfs_bioerror(bp);
  991. }
  992. xfs_buf_iorequest(bp);
  993. return 0;
  994. }
  995. /*
  996. * Wrapper around bdstrat so that we can stop data from going to disk in case
  997. * we are shutting down the filesystem. Typically user data goes thru this
  998. * path; one of the exceptions is the superblock.
  999. */
  1000. void
  1001. xfsbdstrat(
  1002. struct xfs_mount *mp,
  1003. struct xfs_buf *bp)
  1004. {
  1005. if (XFS_FORCED_SHUTDOWN(mp)) {
  1006. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  1007. xfs_bioerror_relse(bp);
  1008. return;
  1009. }
  1010. xfs_buf_iorequest(bp);
  1011. }
  1012. STATIC void
  1013. _xfs_buf_ioend(
  1014. xfs_buf_t *bp,
  1015. int schedule)
  1016. {
  1017. if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
  1018. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  1019. xfs_buf_ioend(bp, schedule);
  1020. }
  1021. }
  1022. STATIC void
  1023. xfs_buf_bio_end_io(
  1024. struct bio *bio,
  1025. int error)
  1026. {
  1027. xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
  1028. unsigned int blocksize = bp->b_target->bt_bsize;
  1029. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  1030. xfs_buf_ioerror(bp, -error);
  1031. if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
  1032. invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
  1033. do {
  1034. struct page *page = bvec->bv_page;
  1035. ASSERT(!PagePrivate(page));
  1036. if (unlikely(bp->b_error)) {
  1037. if (bp->b_flags & XBF_READ)
  1038. ClearPageUptodate(page);
  1039. } else if (blocksize >= PAGE_CACHE_SIZE) {
  1040. SetPageUptodate(page);
  1041. } else if (!PagePrivate(page) &&
  1042. (bp->b_flags & _XBF_PAGE_CACHE)) {
  1043. set_page_region(page, bvec->bv_offset, bvec->bv_len);
  1044. }
  1045. if (--bvec >= bio->bi_io_vec)
  1046. prefetchw(&bvec->bv_page->flags);
  1047. if (bp->b_flags & _XBF_PAGE_LOCKED)
  1048. unlock_page(page);
  1049. } while (bvec >= bio->bi_io_vec);
  1050. _xfs_buf_ioend(bp, 1);
  1051. bio_put(bio);
  1052. }
  1053. STATIC void
  1054. _xfs_buf_ioapply(
  1055. xfs_buf_t *bp)
  1056. {
  1057. int rw, map_i, total_nr_pages, nr_pages;
  1058. struct bio *bio;
  1059. int offset = bp->b_offset;
  1060. int size = bp->b_count_desired;
  1061. sector_t sector = bp->b_bn;
  1062. unsigned int blocksize = bp->b_target->bt_bsize;
  1063. total_nr_pages = bp->b_page_count;
  1064. map_i = 0;
  1065. if (bp->b_flags & XBF_ORDERED) {
  1066. ASSERT(!(bp->b_flags & XBF_READ));
  1067. rw = WRITE_BARRIER;
  1068. } else if (bp->b_flags & XBF_LOG_BUFFER) {
  1069. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1070. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1071. rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
  1072. } else if (bp->b_flags & _XBF_RUN_QUEUES) {
  1073. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1074. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1075. rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
  1076. } else {
  1077. rw = (bp->b_flags & XBF_WRITE) ? WRITE :
  1078. (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
  1079. }
  1080. /* Special code path for reading a sub page size buffer in --
  1081. * we populate up the whole page, and hence the other metadata
  1082. * in the same page. This optimization is only valid when the
  1083. * filesystem block size is not smaller than the page size.
  1084. */
  1085. if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
  1086. ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
  1087. (XBF_READ|_XBF_PAGE_LOCKED)) &&
  1088. (blocksize >= PAGE_CACHE_SIZE)) {
  1089. bio = bio_alloc(GFP_NOIO, 1);
  1090. bio->bi_bdev = bp->b_target->bt_bdev;
  1091. bio->bi_sector = sector - (offset >> BBSHIFT);
  1092. bio->bi_end_io = xfs_buf_bio_end_io;
  1093. bio->bi_private = bp;
  1094. bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
  1095. size = 0;
  1096. atomic_inc(&bp->b_io_remaining);
  1097. goto submit_io;
  1098. }
  1099. next_chunk:
  1100. atomic_inc(&bp->b_io_remaining);
  1101. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1102. if (nr_pages > total_nr_pages)
  1103. nr_pages = total_nr_pages;
  1104. bio = bio_alloc(GFP_NOIO, nr_pages);
  1105. bio->bi_bdev = bp->b_target->bt_bdev;
  1106. bio->bi_sector = sector;
  1107. bio->bi_end_io = xfs_buf_bio_end_io;
  1108. bio->bi_private = bp;
  1109. for (; size && nr_pages; nr_pages--, map_i++) {
  1110. int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
  1111. if (nbytes > size)
  1112. nbytes = size;
  1113. rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
  1114. if (rbytes < nbytes)
  1115. break;
  1116. offset = 0;
  1117. sector += nbytes >> BBSHIFT;
  1118. size -= nbytes;
  1119. total_nr_pages--;
  1120. }
  1121. submit_io:
  1122. if (likely(bio->bi_size)) {
  1123. if (xfs_buf_is_vmapped(bp)) {
  1124. flush_kernel_vmap_range(bp->b_addr,
  1125. xfs_buf_vmap_len(bp));
  1126. }
  1127. submit_bio(rw, bio);
  1128. if (size)
  1129. goto next_chunk;
  1130. } else {
  1131. /*
  1132. * if we get here, no pages were added to the bio. However,
  1133. * we can't just error out here - if the pages are locked then
  1134. * we have to unlock them otherwise we can hang on a later
  1135. * access to the page.
  1136. */
  1137. xfs_buf_ioerror(bp, EIO);
  1138. if (bp->b_flags & _XBF_PAGE_LOCKED) {
  1139. int i;
  1140. for (i = 0; i < bp->b_page_count; i++)
  1141. unlock_page(bp->b_pages[i]);
  1142. }
  1143. bio_put(bio);
  1144. }
  1145. }
  1146. int
  1147. xfs_buf_iorequest(
  1148. xfs_buf_t *bp)
  1149. {
  1150. trace_xfs_buf_iorequest(bp, _RET_IP_);
  1151. if (bp->b_flags & XBF_DELWRI) {
  1152. xfs_buf_delwri_queue(bp, 1);
  1153. return 0;
  1154. }
  1155. if (bp->b_flags & XBF_WRITE) {
  1156. xfs_buf_wait_unpin(bp);
  1157. }
  1158. xfs_buf_hold(bp);
  1159. /* Set the count to 1 initially, this will stop an I/O
  1160. * completion callout which happens before we have started
  1161. * all the I/O from calling xfs_buf_ioend too early.
  1162. */
  1163. atomic_set(&bp->b_io_remaining, 1);
  1164. _xfs_buf_ioapply(bp);
  1165. _xfs_buf_ioend(bp, 0);
  1166. xfs_buf_rele(bp);
  1167. return 0;
  1168. }
  1169. /*
  1170. * Waits for I/O to complete on the buffer supplied.
  1171. * It returns immediately if no I/O is pending.
  1172. * It returns the I/O error code, if any, or 0 if there was no error.
  1173. */
  1174. int
  1175. xfs_buf_iowait(
  1176. xfs_buf_t *bp)
  1177. {
  1178. trace_xfs_buf_iowait(bp, _RET_IP_);
  1179. if (atomic_read(&bp->b_io_remaining))
  1180. blk_run_address_space(bp->b_target->bt_mapping);
  1181. wait_for_completion(&bp->b_iowait);
  1182. trace_xfs_buf_iowait_done(bp, _RET_IP_);
  1183. return bp->b_error;
  1184. }
  1185. xfs_caddr_t
  1186. xfs_buf_offset(
  1187. xfs_buf_t *bp,
  1188. size_t offset)
  1189. {
  1190. struct page *page;
  1191. if (bp->b_flags & XBF_MAPPED)
  1192. return XFS_BUF_PTR(bp) + offset;
  1193. offset += bp->b_offset;
  1194. page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
  1195. return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
  1196. }
  1197. /*
  1198. * Move data into or out of a buffer.
  1199. */
  1200. void
  1201. xfs_buf_iomove(
  1202. xfs_buf_t *bp, /* buffer to process */
  1203. size_t boff, /* starting buffer offset */
  1204. size_t bsize, /* length to copy */
  1205. void *data, /* data address */
  1206. xfs_buf_rw_t mode) /* read/write/zero flag */
  1207. {
  1208. size_t bend, cpoff, csize;
  1209. struct page *page;
  1210. bend = boff + bsize;
  1211. while (boff < bend) {
  1212. page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
  1213. cpoff = xfs_buf_poff(boff + bp->b_offset);
  1214. csize = min_t(size_t,
  1215. PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
  1216. ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
  1217. switch (mode) {
  1218. case XBRW_ZERO:
  1219. memset(page_address(page) + cpoff, 0, csize);
  1220. break;
  1221. case XBRW_READ:
  1222. memcpy(data, page_address(page) + cpoff, csize);
  1223. break;
  1224. case XBRW_WRITE:
  1225. memcpy(page_address(page) + cpoff, data, csize);
  1226. }
  1227. boff += csize;
  1228. data += csize;
  1229. }
  1230. }
  1231. /*
  1232. * Handling of buffer targets (buftargs).
  1233. */
  1234. /*
  1235. * Wait for any bufs with callbacks that have been submitted but
  1236. * have not yet returned... walk the hash list for the target.
  1237. */
  1238. void
  1239. xfs_wait_buftarg(
  1240. xfs_buftarg_t *btp)
  1241. {
  1242. xfs_buf_t *bp, *n;
  1243. xfs_bufhash_t *hash;
  1244. uint i;
  1245. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1246. hash = &btp->bt_hash[i];
  1247. again:
  1248. spin_lock(&hash->bh_lock);
  1249. list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
  1250. ASSERT(btp == bp->b_target);
  1251. if (!(bp->b_flags & XBF_FS_MANAGED)) {
  1252. spin_unlock(&hash->bh_lock);
  1253. /*
  1254. * Catch superblock reference count leaks
  1255. * immediately
  1256. */
  1257. BUG_ON(bp->b_bn == 0);
  1258. delay(100);
  1259. goto again;
  1260. }
  1261. }
  1262. spin_unlock(&hash->bh_lock);
  1263. }
  1264. }
  1265. /*
  1266. * Allocate buffer hash table for a given target.
  1267. * For devices containing metadata (i.e. not the log/realtime devices)
  1268. * we need to allocate a much larger hash table.
  1269. */
  1270. STATIC void
  1271. xfs_alloc_bufhash(
  1272. xfs_buftarg_t *btp,
  1273. int external)
  1274. {
  1275. unsigned int i;
  1276. btp->bt_hashshift = external ? 3 : 12; /* 8 or 4096 buckets */
  1277. btp->bt_hash = kmem_zalloc_large((1 << btp->bt_hashshift) *
  1278. sizeof(xfs_bufhash_t));
  1279. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1280. spin_lock_init(&btp->bt_hash[i].bh_lock);
  1281. INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
  1282. }
  1283. }
  1284. STATIC void
  1285. xfs_free_bufhash(
  1286. xfs_buftarg_t *btp)
  1287. {
  1288. kmem_free_large(btp->bt_hash);
  1289. btp->bt_hash = NULL;
  1290. }
  1291. /*
  1292. * buftarg list for delwrite queue processing
  1293. */
  1294. static LIST_HEAD(xfs_buftarg_list);
  1295. static DEFINE_SPINLOCK(xfs_buftarg_lock);
  1296. STATIC void
  1297. xfs_register_buftarg(
  1298. xfs_buftarg_t *btp)
  1299. {
  1300. spin_lock(&xfs_buftarg_lock);
  1301. list_add(&btp->bt_list, &xfs_buftarg_list);
  1302. spin_unlock(&xfs_buftarg_lock);
  1303. }
  1304. STATIC void
  1305. xfs_unregister_buftarg(
  1306. xfs_buftarg_t *btp)
  1307. {
  1308. spin_lock(&xfs_buftarg_lock);
  1309. list_del(&btp->bt_list);
  1310. spin_unlock(&xfs_buftarg_lock);
  1311. }
  1312. void
  1313. xfs_free_buftarg(
  1314. struct xfs_mount *mp,
  1315. struct xfs_buftarg *btp)
  1316. {
  1317. xfs_flush_buftarg(btp, 1);
  1318. if (mp->m_flags & XFS_MOUNT_BARRIER)
  1319. xfs_blkdev_issue_flush(btp);
  1320. xfs_free_bufhash(btp);
  1321. iput(btp->bt_mapping->host);
  1322. /* Unregister the buftarg first so that we don't get a
  1323. * wakeup finding a non-existent task
  1324. */
  1325. xfs_unregister_buftarg(btp);
  1326. kthread_stop(btp->bt_task);
  1327. kmem_free(btp);
  1328. }
  1329. STATIC int
  1330. xfs_setsize_buftarg_flags(
  1331. xfs_buftarg_t *btp,
  1332. unsigned int blocksize,
  1333. unsigned int sectorsize,
  1334. int verbose)
  1335. {
  1336. btp->bt_bsize = blocksize;
  1337. btp->bt_sshift = ffs(sectorsize) - 1;
  1338. btp->bt_smask = sectorsize - 1;
  1339. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1340. printk(KERN_WARNING
  1341. "XFS: Cannot set_blocksize to %u on device %s\n",
  1342. sectorsize, XFS_BUFTARG_NAME(btp));
  1343. return EINVAL;
  1344. }
  1345. if (verbose &&
  1346. (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
  1347. printk(KERN_WARNING
  1348. "XFS: %u byte sectors in use on device %s. "
  1349. "This is suboptimal; %u or greater is ideal.\n",
  1350. sectorsize, XFS_BUFTARG_NAME(btp),
  1351. (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
  1352. }
  1353. return 0;
  1354. }
  1355. /*
  1356. * When allocating the initial buffer target we have not yet
  1357. * read in the superblock, so don't know what sized sectors
  1358. * are being used is at this early stage. Play safe.
  1359. */
  1360. STATIC int
  1361. xfs_setsize_buftarg_early(
  1362. xfs_buftarg_t *btp,
  1363. struct block_device *bdev)
  1364. {
  1365. return xfs_setsize_buftarg_flags(btp,
  1366. PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
  1367. }
  1368. int
  1369. xfs_setsize_buftarg(
  1370. xfs_buftarg_t *btp,
  1371. unsigned int blocksize,
  1372. unsigned int sectorsize)
  1373. {
  1374. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1375. }
  1376. STATIC int
  1377. xfs_mapping_buftarg(
  1378. xfs_buftarg_t *btp,
  1379. struct block_device *bdev)
  1380. {
  1381. struct backing_dev_info *bdi;
  1382. struct inode *inode;
  1383. struct address_space *mapping;
  1384. static const struct address_space_operations mapping_aops = {
  1385. .sync_page = block_sync_page,
  1386. .migratepage = fail_migrate_page,
  1387. };
  1388. inode = new_inode(bdev->bd_inode->i_sb);
  1389. if (!inode) {
  1390. printk(KERN_WARNING
  1391. "XFS: Cannot allocate mapping inode for device %s\n",
  1392. XFS_BUFTARG_NAME(btp));
  1393. return ENOMEM;
  1394. }
  1395. inode->i_mode = S_IFBLK;
  1396. inode->i_bdev = bdev;
  1397. inode->i_rdev = bdev->bd_dev;
  1398. bdi = blk_get_backing_dev_info(bdev);
  1399. if (!bdi)
  1400. bdi = &default_backing_dev_info;
  1401. mapping = &inode->i_data;
  1402. mapping->a_ops = &mapping_aops;
  1403. mapping->backing_dev_info = bdi;
  1404. mapping_set_gfp_mask(mapping, GFP_NOFS);
  1405. btp->bt_mapping = mapping;
  1406. return 0;
  1407. }
  1408. STATIC int
  1409. xfs_alloc_delwrite_queue(
  1410. xfs_buftarg_t *btp,
  1411. const char *fsname)
  1412. {
  1413. int error = 0;
  1414. INIT_LIST_HEAD(&btp->bt_list);
  1415. INIT_LIST_HEAD(&btp->bt_delwrite_queue);
  1416. spin_lock_init(&btp->bt_delwrite_lock);
  1417. btp->bt_flags = 0;
  1418. btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
  1419. if (IS_ERR(btp->bt_task)) {
  1420. error = PTR_ERR(btp->bt_task);
  1421. goto out_error;
  1422. }
  1423. xfs_register_buftarg(btp);
  1424. out_error:
  1425. return error;
  1426. }
  1427. xfs_buftarg_t *
  1428. xfs_alloc_buftarg(
  1429. struct block_device *bdev,
  1430. int external,
  1431. const char *fsname)
  1432. {
  1433. xfs_buftarg_t *btp;
  1434. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1435. btp->bt_dev = bdev->bd_dev;
  1436. btp->bt_bdev = bdev;
  1437. if (xfs_setsize_buftarg_early(btp, bdev))
  1438. goto error;
  1439. if (xfs_mapping_buftarg(btp, bdev))
  1440. goto error;
  1441. if (xfs_alloc_delwrite_queue(btp, fsname))
  1442. goto error;
  1443. xfs_alloc_bufhash(btp, external);
  1444. return btp;
  1445. error:
  1446. kmem_free(btp);
  1447. return NULL;
  1448. }
  1449. /*
  1450. * Delayed write buffer handling
  1451. */
  1452. STATIC void
  1453. xfs_buf_delwri_queue(
  1454. xfs_buf_t *bp,
  1455. int unlock)
  1456. {
  1457. struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
  1458. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1459. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1460. ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
  1461. spin_lock(dwlk);
  1462. /* If already in the queue, dequeue and place at tail */
  1463. if (!list_empty(&bp->b_list)) {
  1464. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1465. if (unlock)
  1466. atomic_dec(&bp->b_hold);
  1467. list_del(&bp->b_list);
  1468. }
  1469. if (list_empty(dwq)) {
  1470. /* start xfsbufd as it is about to have something to do */
  1471. wake_up_process(bp->b_target->bt_task);
  1472. }
  1473. bp->b_flags |= _XBF_DELWRI_Q;
  1474. list_add_tail(&bp->b_list, dwq);
  1475. bp->b_queuetime = jiffies;
  1476. spin_unlock(dwlk);
  1477. if (unlock)
  1478. xfs_buf_unlock(bp);
  1479. }
  1480. void
  1481. xfs_buf_delwri_dequeue(
  1482. xfs_buf_t *bp)
  1483. {
  1484. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1485. int dequeued = 0;
  1486. spin_lock(dwlk);
  1487. if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
  1488. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1489. list_del_init(&bp->b_list);
  1490. dequeued = 1;
  1491. }
  1492. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
  1493. spin_unlock(dwlk);
  1494. if (dequeued)
  1495. xfs_buf_rele(bp);
  1496. trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
  1497. }
  1498. /*
  1499. * If a delwri buffer needs to be pushed before it has aged out, then promote
  1500. * it to the head of the delwri queue so that it will be flushed on the next
  1501. * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
  1502. * than the age currently needed to flush the buffer. Hence the next time the
  1503. * xfsbufd sees it is guaranteed to be considered old enough to flush.
  1504. */
  1505. void
  1506. xfs_buf_delwri_promote(
  1507. struct xfs_buf *bp)
  1508. {
  1509. struct xfs_buftarg *btp = bp->b_target;
  1510. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
  1511. ASSERT(bp->b_flags & XBF_DELWRI);
  1512. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1513. /*
  1514. * Check the buffer age before locking the delayed write queue as we
  1515. * don't need to promote buffers that are already past the flush age.
  1516. */
  1517. if (bp->b_queuetime < jiffies - age)
  1518. return;
  1519. bp->b_queuetime = jiffies - age;
  1520. spin_lock(&btp->bt_delwrite_lock);
  1521. list_move(&bp->b_list, &btp->bt_delwrite_queue);
  1522. spin_unlock(&btp->bt_delwrite_lock);
  1523. }
  1524. STATIC void
  1525. xfs_buf_runall_queues(
  1526. struct workqueue_struct *queue)
  1527. {
  1528. flush_workqueue(queue);
  1529. }
  1530. STATIC int
  1531. xfsbufd_wakeup(
  1532. struct shrinker *shrink,
  1533. int priority,
  1534. gfp_t mask)
  1535. {
  1536. xfs_buftarg_t *btp;
  1537. spin_lock(&xfs_buftarg_lock);
  1538. list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
  1539. if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
  1540. continue;
  1541. if (list_empty(&btp->bt_delwrite_queue))
  1542. continue;
  1543. set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
  1544. wake_up_process(btp->bt_task);
  1545. }
  1546. spin_unlock(&xfs_buftarg_lock);
  1547. return 0;
  1548. }
  1549. /*
  1550. * Move as many buffers as specified to the supplied list
  1551. * idicating if we skipped any buffers to prevent deadlocks.
  1552. */
  1553. STATIC int
  1554. xfs_buf_delwri_split(
  1555. xfs_buftarg_t *target,
  1556. struct list_head *list,
  1557. unsigned long age)
  1558. {
  1559. xfs_buf_t *bp, *n;
  1560. struct list_head *dwq = &target->bt_delwrite_queue;
  1561. spinlock_t *dwlk = &target->bt_delwrite_lock;
  1562. int skipped = 0;
  1563. int force;
  1564. force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1565. INIT_LIST_HEAD(list);
  1566. spin_lock(dwlk);
  1567. list_for_each_entry_safe(bp, n, dwq, b_list) {
  1568. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1569. ASSERT(bp->b_flags & XBF_DELWRI);
  1570. if (!XFS_BUF_ISPINNED(bp) && !xfs_buf_cond_lock(bp)) {
  1571. if (!force &&
  1572. time_before(jiffies, bp->b_queuetime + age)) {
  1573. xfs_buf_unlock(bp);
  1574. break;
  1575. }
  1576. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
  1577. _XBF_RUN_QUEUES);
  1578. bp->b_flags |= XBF_WRITE;
  1579. list_move_tail(&bp->b_list, list);
  1580. } else
  1581. skipped++;
  1582. }
  1583. spin_unlock(dwlk);
  1584. return skipped;
  1585. }
  1586. /*
  1587. * Compare function is more complex than it needs to be because
  1588. * the return value is only 32 bits and we are doing comparisons
  1589. * on 64 bit values
  1590. */
  1591. static int
  1592. xfs_buf_cmp(
  1593. void *priv,
  1594. struct list_head *a,
  1595. struct list_head *b)
  1596. {
  1597. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1598. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1599. xfs_daddr_t diff;
  1600. diff = ap->b_bn - bp->b_bn;
  1601. if (diff < 0)
  1602. return -1;
  1603. if (diff > 0)
  1604. return 1;
  1605. return 0;
  1606. }
  1607. void
  1608. xfs_buf_delwri_sort(
  1609. xfs_buftarg_t *target,
  1610. struct list_head *list)
  1611. {
  1612. list_sort(NULL, list, xfs_buf_cmp);
  1613. }
  1614. STATIC int
  1615. xfsbufd(
  1616. void *data)
  1617. {
  1618. xfs_buftarg_t *target = (xfs_buftarg_t *)data;
  1619. current->flags |= PF_MEMALLOC;
  1620. set_freezable();
  1621. do {
  1622. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
  1623. long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
  1624. int count = 0;
  1625. struct list_head tmp;
  1626. if (unlikely(freezing(current))) {
  1627. set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1628. refrigerator();
  1629. } else {
  1630. clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1631. }
  1632. /* sleep for a long time if there is nothing to do. */
  1633. if (list_empty(&target->bt_delwrite_queue))
  1634. tout = MAX_SCHEDULE_TIMEOUT;
  1635. schedule_timeout_interruptible(tout);
  1636. xfs_buf_delwri_split(target, &tmp, age);
  1637. list_sort(NULL, &tmp, xfs_buf_cmp);
  1638. while (!list_empty(&tmp)) {
  1639. struct xfs_buf *bp;
  1640. bp = list_first_entry(&tmp, struct xfs_buf, b_list);
  1641. list_del_init(&bp->b_list);
  1642. xfs_bdstrat_cb(bp);
  1643. count++;
  1644. }
  1645. if (count)
  1646. blk_run_address_space(target->bt_mapping);
  1647. } while (!kthread_should_stop());
  1648. return 0;
  1649. }
  1650. /*
  1651. * Go through all incore buffers, and release buffers if they belong to
  1652. * the given device. This is used in filesystem error handling to
  1653. * preserve the consistency of its metadata.
  1654. */
  1655. int
  1656. xfs_flush_buftarg(
  1657. xfs_buftarg_t *target,
  1658. int wait)
  1659. {
  1660. xfs_buf_t *bp;
  1661. int pincount = 0;
  1662. LIST_HEAD(tmp_list);
  1663. LIST_HEAD(wait_list);
  1664. xfs_buf_runall_queues(xfsconvertd_workqueue);
  1665. xfs_buf_runall_queues(xfsdatad_workqueue);
  1666. xfs_buf_runall_queues(xfslogd_workqueue);
  1667. set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1668. pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
  1669. /*
  1670. * Dropped the delayed write list lock, now walk the temporary list.
  1671. * All I/O is issued async and then if we need to wait for completion
  1672. * we do that after issuing all the IO.
  1673. */
  1674. list_sort(NULL, &tmp_list, xfs_buf_cmp);
  1675. while (!list_empty(&tmp_list)) {
  1676. bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
  1677. ASSERT(target == bp->b_target);
  1678. list_del_init(&bp->b_list);
  1679. if (wait) {
  1680. bp->b_flags &= ~XBF_ASYNC;
  1681. list_add(&bp->b_list, &wait_list);
  1682. }
  1683. xfs_bdstrat_cb(bp);
  1684. }
  1685. if (wait) {
  1686. /* Expedite and wait for IO to complete. */
  1687. blk_run_address_space(target->bt_mapping);
  1688. while (!list_empty(&wait_list)) {
  1689. bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
  1690. list_del_init(&bp->b_list);
  1691. xfs_iowait(bp);
  1692. xfs_buf_relse(bp);
  1693. }
  1694. }
  1695. return pincount;
  1696. }
  1697. int __init
  1698. xfs_buf_init(void)
  1699. {
  1700. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1701. KM_ZONE_HWALIGN, NULL);
  1702. if (!xfs_buf_zone)
  1703. goto out;
  1704. xfslogd_workqueue = alloc_workqueue("xfslogd",
  1705. WQ_RESCUER | WQ_HIGHPRI, 1);
  1706. if (!xfslogd_workqueue)
  1707. goto out_free_buf_zone;
  1708. xfsdatad_workqueue = create_workqueue("xfsdatad");
  1709. if (!xfsdatad_workqueue)
  1710. goto out_destroy_xfslogd_workqueue;
  1711. xfsconvertd_workqueue = create_workqueue("xfsconvertd");
  1712. if (!xfsconvertd_workqueue)
  1713. goto out_destroy_xfsdatad_workqueue;
  1714. register_shrinker(&xfs_buf_shake);
  1715. return 0;
  1716. out_destroy_xfsdatad_workqueue:
  1717. destroy_workqueue(xfsdatad_workqueue);
  1718. out_destroy_xfslogd_workqueue:
  1719. destroy_workqueue(xfslogd_workqueue);
  1720. out_free_buf_zone:
  1721. kmem_zone_destroy(xfs_buf_zone);
  1722. out:
  1723. return -ENOMEM;
  1724. }
  1725. void
  1726. xfs_buf_terminate(void)
  1727. {
  1728. unregister_shrinker(&xfs_buf_shake);
  1729. destroy_workqueue(xfsconvertd_workqueue);
  1730. destroy_workqueue(xfsdatad_workqueue);
  1731. destroy_workqueue(xfslogd_workqueue);
  1732. kmem_zone_destroy(xfs_buf_zone);
  1733. }
  1734. #ifdef CONFIG_KDB_MODULES
  1735. struct list_head *
  1736. xfs_get_buftarg_list(void)
  1737. {
  1738. return &xfs_buftarg_list;
  1739. }
  1740. #endif