xfs_buf.c 46 KB

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