xfs_buf.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  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 "xfs_format.h"
  37. #include "xfs_log_format.h"
  38. #include "xfs_trans_resv.h"
  39. #include "xfs_sb.h"
  40. #include "xfs_mount.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_log.h"
  43. static kmem_zone_t *xfs_buf_zone;
  44. #ifdef XFS_BUF_LOCK_TRACKING
  45. # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
  46. # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
  47. # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
  48. #else
  49. # define XB_SET_OWNER(bp) do { } while (0)
  50. # define XB_CLEAR_OWNER(bp) do { } while (0)
  51. # define XB_GET_OWNER(bp) do { } while (0)
  52. #endif
  53. #define xb_to_gfp(flags) \
  54. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
  55. static inline int
  56. xfs_buf_is_vmapped(
  57. struct xfs_buf *bp)
  58. {
  59. /*
  60. * Return true if the buffer is vmapped.
  61. *
  62. * b_addr is null if the buffer is not mapped, but the code is clever
  63. * enough to know it doesn't have to map a single page, so the check has
  64. * to be both for b_addr and bp->b_page_count > 1.
  65. */
  66. return bp->b_addr && bp->b_page_count > 1;
  67. }
  68. static inline int
  69. xfs_buf_vmap_len(
  70. struct xfs_buf *bp)
  71. {
  72. return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
  73. }
  74. /*
  75. * Bump the I/O in flight count on the buftarg if we haven't yet done so for
  76. * this buffer. The count is incremented once per buffer (per hold cycle)
  77. * because the corresponding decrement is deferred to buffer release. Buffers
  78. * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
  79. * tracking adds unnecessary overhead. This is used for sychronization purposes
  80. * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
  81. * in-flight buffers.
  82. *
  83. * Buffers that are never released (e.g., superblock, iclog buffers) must set
  84. * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
  85. * never reaches zero and unmount hangs indefinitely.
  86. */
  87. static inline void
  88. xfs_buf_ioacct_inc(
  89. struct xfs_buf *bp)
  90. {
  91. if (bp->b_flags & (XBF_NO_IOACCT|_XBF_IN_FLIGHT))
  92. return;
  93. ASSERT(bp->b_flags & XBF_ASYNC);
  94. bp->b_flags |= _XBF_IN_FLIGHT;
  95. percpu_counter_inc(&bp->b_target->bt_io_count);
  96. }
  97. /*
  98. * Clear the in-flight state on a buffer about to be released to the LRU or
  99. * freed and unaccount from the buftarg.
  100. */
  101. static inline void
  102. xfs_buf_ioacct_dec(
  103. struct xfs_buf *bp)
  104. {
  105. if (!(bp->b_flags & _XBF_IN_FLIGHT))
  106. return;
  107. bp->b_flags &= ~_XBF_IN_FLIGHT;
  108. percpu_counter_dec(&bp->b_target->bt_io_count);
  109. }
  110. /*
  111. * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  112. * b_lru_ref count so that the buffer is freed immediately when the buffer
  113. * reference count falls to zero. If the buffer is already on the LRU, we need
  114. * to remove the reference that LRU holds on the buffer.
  115. *
  116. * This prevents build-up of stale buffers on the LRU.
  117. */
  118. void
  119. xfs_buf_stale(
  120. struct xfs_buf *bp)
  121. {
  122. ASSERT(xfs_buf_islocked(bp));
  123. bp->b_flags |= XBF_STALE;
  124. /*
  125. * Clear the delwri status so that a delwri queue walker will not
  126. * flush this buffer to disk now that it is stale. The delwri queue has
  127. * a reference to the buffer, so this is safe to do.
  128. */
  129. bp->b_flags &= ~_XBF_DELWRI_Q;
  130. /*
  131. * Once the buffer is marked stale and unlocked, a subsequent lookup
  132. * could reset b_flags. There is no guarantee that the buffer is
  133. * unaccounted (released to LRU) before that occurs. Drop in-flight
  134. * status now to preserve accounting consistency.
  135. */
  136. xfs_buf_ioacct_dec(bp);
  137. spin_lock(&bp->b_lock);
  138. atomic_set(&bp->b_lru_ref, 0);
  139. if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
  140. (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
  141. atomic_dec(&bp->b_hold);
  142. ASSERT(atomic_read(&bp->b_hold) >= 1);
  143. spin_unlock(&bp->b_lock);
  144. }
  145. static int
  146. xfs_buf_get_maps(
  147. struct xfs_buf *bp,
  148. int map_count)
  149. {
  150. ASSERT(bp->b_maps == NULL);
  151. bp->b_map_count = map_count;
  152. if (map_count == 1) {
  153. bp->b_maps = &bp->__b_map;
  154. return 0;
  155. }
  156. bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
  157. KM_NOFS);
  158. if (!bp->b_maps)
  159. return -ENOMEM;
  160. return 0;
  161. }
  162. /*
  163. * Frees b_pages if it was allocated.
  164. */
  165. static void
  166. xfs_buf_free_maps(
  167. struct xfs_buf *bp)
  168. {
  169. if (bp->b_maps != &bp->__b_map) {
  170. kmem_free(bp->b_maps);
  171. bp->b_maps = NULL;
  172. }
  173. }
  174. struct xfs_buf *
  175. _xfs_buf_alloc(
  176. struct xfs_buftarg *target,
  177. struct xfs_buf_map *map,
  178. int nmaps,
  179. xfs_buf_flags_t flags)
  180. {
  181. struct xfs_buf *bp;
  182. int error;
  183. int i;
  184. bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
  185. if (unlikely(!bp))
  186. return NULL;
  187. /*
  188. * We don't want certain flags to appear in b_flags unless they are
  189. * specifically set by later operations on the buffer.
  190. */
  191. flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
  192. atomic_set(&bp->b_hold, 1);
  193. atomic_set(&bp->b_lru_ref, 1);
  194. init_completion(&bp->b_iowait);
  195. INIT_LIST_HEAD(&bp->b_lru);
  196. INIT_LIST_HEAD(&bp->b_list);
  197. RB_CLEAR_NODE(&bp->b_rbnode);
  198. sema_init(&bp->b_sema, 0); /* held, no waiters */
  199. spin_lock_init(&bp->b_lock);
  200. XB_SET_OWNER(bp);
  201. bp->b_target = target;
  202. bp->b_flags = flags;
  203. /*
  204. * Set length and io_length to the same value initially.
  205. * I/O routines should use io_length, which will be the same in
  206. * most cases but may be reset (e.g. XFS recovery).
  207. */
  208. error = xfs_buf_get_maps(bp, nmaps);
  209. if (error) {
  210. kmem_zone_free(xfs_buf_zone, bp);
  211. return NULL;
  212. }
  213. bp->b_bn = map[0].bm_bn;
  214. bp->b_length = 0;
  215. for (i = 0; i < nmaps; i++) {
  216. bp->b_maps[i].bm_bn = map[i].bm_bn;
  217. bp->b_maps[i].bm_len = map[i].bm_len;
  218. bp->b_length += map[i].bm_len;
  219. }
  220. bp->b_io_length = bp->b_length;
  221. atomic_set(&bp->b_pin_count, 0);
  222. init_waitqueue_head(&bp->b_waiters);
  223. XFS_STATS_INC(target->bt_mount, xb_create);
  224. trace_xfs_buf_init(bp, _RET_IP_);
  225. return bp;
  226. }
  227. /*
  228. * Allocate a page array capable of holding a specified number
  229. * of pages, and point the page buf at it.
  230. */
  231. STATIC int
  232. _xfs_buf_get_pages(
  233. xfs_buf_t *bp,
  234. int page_count)
  235. {
  236. /* Make sure that we have a page list */
  237. if (bp->b_pages == NULL) {
  238. bp->b_page_count = page_count;
  239. if (page_count <= XB_PAGES) {
  240. bp->b_pages = bp->b_page_array;
  241. } else {
  242. bp->b_pages = kmem_alloc(sizeof(struct page *) *
  243. page_count, KM_NOFS);
  244. if (bp->b_pages == NULL)
  245. return -ENOMEM;
  246. }
  247. memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
  248. }
  249. return 0;
  250. }
  251. /*
  252. * Frees b_pages if it was allocated.
  253. */
  254. STATIC void
  255. _xfs_buf_free_pages(
  256. xfs_buf_t *bp)
  257. {
  258. if (bp->b_pages != bp->b_page_array) {
  259. kmem_free(bp->b_pages);
  260. bp->b_pages = NULL;
  261. }
  262. }
  263. /*
  264. * Releases the specified buffer.
  265. *
  266. * The modification state of any associated pages is left unchanged.
  267. * The buffer must not be on any hash - use xfs_buf_rele instead for
  268. * hashed and refcounted buffers
  269. */
  270. void
  271. xfs_buf_free(
  272. xfs_buf_t *bp)
  273. {
  274. trace_xfs_buf_free(bp, _RET_IP_);
  275. ASSERT(list_empty(&bp->b_lru));
  276. if (bp->b_flags & _XBF_PAGES) {
  277. uint i;
  278. if (xfs_buf_is_vmapped(bp))
  279. vm_unmap_ram(bp->b_addr - bp->b_offset,
  280. bp->b_page_count);
  281. for (i = 0; i < bp->b_page_count; i++) {
  282. struct page *page = bp->b_pages[i];
  283. __free_page(page);
  284. }
  285. } else if (bp->b_flags & _XBF_KMEM)
  286. kmem_free(bp->b_addr);
  287. _xfs_buf_free_pages(bp);
  288. xfs_buf_free_maps(bp);
  289. kmem_zone_free(xfs_buf_zone, bp);
  290. }
  291. /*
  292. * Allocates all the pages for buffer in question and builds it's page list.
  293. */
  294. STATIC int
  295. xfs_buf_allocate_memory(
  296. xfs_buf_t *bp,
  297. uint flags)
  298. {
  299. size_t size;
  300. size_t nbytes, offset;
  301. gfp_t gfp_mask = xb_to_gfp(flags);
  302. unsigned short page_count, i;
  303. xfs_off_t start, end;
  304. int error;
  305. /*
  306. * for buffers that are contained within a single page, just allocate
  307. * the memory from the heap - there's no need for the complexity of
  308. * page arrays to keep allocation down to order 0.
  309. */
  310. size = BBTOB(bp->b_length);
  311. if (size < PAGE_SIZE) {
  312. bp->b_addr = kmem_alloc(size, KM_NOFS);
  313. if (!bp->b_addr) {
  314. /* low memory - use alloc_page loop instead */
  315. goto use_alloc_page;
  316. }
  317. if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
  318. ((unsigned long)bp->b_addr & PAGE_MASK)) {
  319. /* b_addr spans two pages - use alloc_page instead */
  320. kmem_free(bp->b_addr);
  321. bp->b_addr = NULL;
  322. goto use_alloc_page;
  323. }
  324. bp->b_offset = offset_in_page(bp->b_addr);
  325. bp->b_pages = bp->b_page_array;
  326. bp->b_pages[0] = virt_to_page(bp->b_addr);
  327. bp->b_page_count = 1;
  328. bp->b_flags |= _XBF_KMEM;
  329. return 0;
  330. }
  331. use_alloc_page:
  332. start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
  333. end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
  334. >> PAGE_SHIFT;
  335. page_count = end - start;
  336. error = _xfs_buf_get_pages(bp, page_count);
  337. if (unlikely(error))
  338. return error;
  339. offset = bp->b_offset;
  340. bp->b_flags |= _XBF_PAGES;
  341. for (i = 0; i < bp->b_page_count; i++) {
  342. struct page *page;
  343. uint retries = 0;
  344. retry:
  345. page = alloc_page(gfp_mask);
  346. if (unlikely(page == NULL)) {
  347. if (flags & XBF_READ_AHEAD) {
  348. bp->b_page_count = i;
  349. error = -ENOMEM;
  350. goto out_free_pages;
  351. }
  352. /*
  353. * This could deadlock.
  354. *
  355. * But until all the XFS lowlevel code is revamped to
  356. * handle buffer allocation failures we can't do much.
  357. */
  358. if (!(++retries % 100))
  359. xfs_err(NULL,
  360. "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
  361. current->comm, current->pid,
  362. __func__, gfp_mask);
  363. XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries);
  364. congestion_wait(BLK_RW_ASYNC, HZ/50);
  365. goto retry;
  366. }
  367. XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found);
  368. nbytes = min_t(size_t, size, PAGE_SIZE - offset);
  369. size -= nbytes;
  370. bp->b_pages[i] = page;
  371. offset = 0;
  372. }
  373. return 0;
  374. out_free_pages:
  375. for (i = 0; i < bp->b_page_count; i++)
  376. __free_page(bp->b_pages[i]);
  377. return error;
  378. }
  379. /*
  380. * Map buffer into kernel address-space if necessary.
  381. */
  382. STATIC int
  383. _xfs_buf_map_pages(
  384. xfs_buf_t *bp,
  385. uint flags)
  386. {
  387. ASSERT(bp->b_flags & _XBF_PAGES);
  388. if (bp->b_page_count == 1) {
  389. /* A single page buffer is always mappable */
  390. bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
  391. } else if (flags & XBF_UNMAPPED) {
  392. bp->b_addr = NULL;
  393. } else {
  394. int retried = 0;
  395. unsigned noio_flag;
  396. /*
  397. * vm_map_ram() will allocate auxillary structures (e.g.
  398. * pagetables) with GFP_KERNEL, yet we are likely to be under
  399. * GFP_NOFS context here. Hence we need to tell memory reclaim
  400. * that we are in such a context via PF_MEMALLOC_NOIO to prevent
  401. * memory reclaim re-entering the filesystem here and
  402. * potentially deadlocking.
  403. */
  404. noio_flag = memalloc_noio_save();
  405. do {
  406. bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
  407. -1, PAGE_KERNEL);
  408. if (bp->b_addr)
  409. break;
  410. vm_unmap_aliases();
  411. } while (retried++ <= 1);
  412. memalloc_noio_restore(noio_flag);
  413. if (!bp->b_addr)
  414. return -ENOMEM;
  415. bp->b_addr += bp->b_offset;
  416. }
  417. return 0;
  418. }
  419. /*
  420. * Finding and Reading Buffers
  421. */
  422. /*
  423. * Look up, and creates if absent, a lockable buffer for
  424. * a given range of an inode. The buffer is returned
  425. * locked. No I/O is implied by this call.
  426. */
  427. xfs_buf_t *
  428. _xfs_buf_find(
  429. struct xfs_buftarg *btp,
  430. struct xfs_buf_map *map,
  431. int nmaps,
  432. xfs_buf_flags_t flags,
  433. xfs_buf_t *new_bp)
  434. {
  435. struct xfs_perag *pag;
  436. struct rb_node **rbp;
  437. struct rb_node *parent;
  438. xfs_buf_t *bp;
  439. xfs_daddr_t blkno = map[0].bm_bn;
  440. xfs_daddr_t eofs;
  441. int numblks = 0;
  442. int i;
  443. for (i = 0; i < nmaps; i++)
  444. numblks += map[i].bm_len;
  445. /* Check for IOs smaller than the sector size / not sector aligned */
  446. ASSERT(!(BBTOB(numblks) < btp->bt_meta_sectorsize));
  447. ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_meta_sectormask));
  448. /*
  449. * Corrupted block numbers can get through to here, unfortunately, so we
  450. * have to check that the buffer falls within the filesystem bounds.
  451. */
  452. eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
  453. if (blkno < 0 || blkno >= eofs) {
  454. /*
  455. * XXX (dgc): we should really be returning -EFSCORRUPTED here,
  456. * but none of the higher level infrastructure supports
  457. * returning a specific error on buffer lookup failures.
  458. */
  459. xfs_alert(btp->bt_mount,
  460. "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
  461. __func__, blkno, eofs);
  462. WARN_ON(1);
  463. return NULL;
  464. }
  465. /* get tree root */
  466. pag = xfs_perag_get(btp->bt_mount,
  467. xfs_daddr_to_agno(btp->bt_mount, blkno));
  468. /* walk tree */
  469. spin_lock(&pag->pag_buf_lock);
  470. rbp = &pag->pag_buf_tree.rb_node;
  471. parent = NULL;
  472. bp = NULL;
  473. while (*rbp) {
  474. parent = *rbp;
  475. bp = rb_entry(parent, struct xfs_buf, b_rbnode);
  476. if (blkno < bp->b_bn)
  477. rbp = &(*rbp)->rb_left;
  478. else if (blkno > bp->b_bn)
  479. rbp = &(*rbp)->rb_right;
  480. else {
  481. /*
  482. * found a block number match. If the range doesn't
  483. * match, the only way this is allowed is if the buffer
  484. * in the cache is stale and the transaction that made
  485. * it stale has not yet committed. i.e. we are
  486. * reallocating a busy extent. Skip this buffer and
  487. * continue searching to the right for an exact match.
  488. */
  489. if (bp->b_length != numblks) {
  490. ASSERT(bp->b_flags & XBF_STALE);
  491. rbp = &(*rbp)->rb_right;
  492. continue;
  493. }
  494. atomic_inc(&bp->b_hold);
  495. goto found;
  496. }
  497. }
  498. /* No match found */
  499. if (new_bp) {
  500. rb_link_node(&new_bp->b_rbnode, parent, rbp);
  501. rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
  502. /* the buffer keeps the perag reference until it is freed */
  503. new_bp->b_pag = pag;
  504. spin_unlock(&pag->pag_buf_lock);
  505. } else {
  506. XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
  507. spin_unlock(&pag->pag_buf_lock);
  508. xfs_perag_put(pag);
  509. }
  510. return new_bp;
  511. found:
  512. spin_unlock(&pag->pag_buf_lock);
  513. xfs_perag_put(pag);
  514. if (!xfs_buf_trylock(bp)) {
  515. if (flags & XBF_TRYLOCK) {
  516. xfs_buf_rele(bp);
  517. XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
  518. return NULL;
  519. }
  520. xfs_buf_lock(bp);
  521. XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
  522. }
  523. /*
  524. * if the buffer is stale, clear all the external state associated with
  525. * it. We need to keep flags such as how we allocated the buffer memory
  526. * intact here.
  527. */
  528. if (bp->b_flags & XBF_STALE) {
  529. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  530. ASSERT(bp->b_iodone == NULL);
  531. bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
  532. bp->b_ops = NULL;
  533. }
  534. trace_xfs_buf_find(bp, flags, _RET_IP_);
  535. XFS_STATS_INC(btp->bt_mount, xb_get_locked);
  536. return bp;
  537. }
  538. /*
  539. * Assembles a buffer covering the specified range. The code is optimised for
  540. * cache hits, as metadata intensive workloads will see 3 orders of magnitude
  541. * more hits than misses.
  542. */
  543. struct xfs_buf *
  544. xfs_buf_get_map(
  545. struct xfs_buftarg *target,
  546. struct xfs_buf_map *map,
  547. int nmaps,
  548. xfs_buf_flags_t flags)
  549. {
  550. struct xfs_buf *bp;
  551. struct xfs_buf *new_bp;
  552. int error = 0;
  553. bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
  554. if (likely(bp))
  555. goto found;
  556. new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
  557. if (unlikely(!new_bp))
  558. return NULL;
  559. error = xfs_buf_allocate_memory(new_bp, flags);
  560. if (error) {
  561. xfs_buf_free(new_bp);
  562. return NULL;
  563. }
  564. bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
  565. if (!bp) {
  566. xfs_buf_free(new_bp);
  567. return NULL;
  568. }
  569. if (bp != new_bp)
  570. xfs_buf_free(new_bp);
  571. found:
  572. if (!bp->b_addr) {
  573. error = _xfs_buf_map_pages(bp, flags);
  574. if (unlikely(error)) {
  575. xfs_warn(target->bt_mount,
  576. "%s: failed to map pagesn", __func__);
  577. xfs_buf_relse(bp);
  578. return NULL;
  579. }
  580. }
  581. /*
  582. * Clear b_error if this is a lookup from a caller that doesn't expect
  583. * valid data to be found in the buffer.
  584. */
  585. if (!(flags & XBF_READ))
  586. xfs_buf_ioerror(bp, 0);
  587. XFS_STATS_INC(target->bt_mount, xb_get);
  588. trace_xfs_buf_get(bp, flags, _RET_IP_);
  589. return bp;
  590. }
  591. STATIC int
  592. _xfs_buf_read(
  593. xfs_buf_t *bp,
  594. xfs_buf_flags_t flags)
  595. {
  596. ASSERT(!(flags & XBF_WRITE));
  597. ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
  598. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
  599. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
  600. if (flags & XBF_ASYNC) {
  601. xfs_buf_submit(bp);
  602. return 0;
  603. }
  604. return xfs_buf_submit_wait(bp);
  605. }
  606. xfs_buf_t *
  607. xfs_buf_read_map(
  608. struct xfs_buftarg *target,
  609. struct xfs_buf_map *map,
  610. int nmaps,
  611. xfs_buf_flags_t flags,
  612. const struct xfs_buf_ops *ops)
  613. {
  614. struct xfs_buf *bp;
  615. flags |= XBF_READ;
  616. bp = xfs_buf_get_map(target, map, nmaps, flags);
  617. if (bp) {
  618. trace_xfs_buf_read(bp, flags, _RET_IP_);
  619. if (!(bp->b_flags & XBF_DONE)) {
  620. XFS_STATS_INC(target->bt_mount, xb_get_read);
  621. bp->b_ops = ops;
  622. _xfs_buf_read(bp, flags);
  623. } else if (flags & XBF_ASYNC) {
  624. /*
  625. * Read ahead call which is already satisfied,
  626. * drop the buffer
  627. */
  628. xfs_buf_relse(bp);
  629. return NULL;
  630. } else {
  631. /* We do not want read in the flags */
  632. bp->b_flags &= ~XBF_READ;
  633. }
  634. }
  635. return bp;
  636. }
  637. /*
  638. * If we are not low on memory then do the readahead in a deadlock
  639. * safe manner.
  640. */
  641. void
  642. xfs_buf_readahead_map(
  643. struct xfs_buftarg *target,
  644. struct xfs_buf_map *map,
  645. int nmaps,
  646. const struct xfs_buf_ops *ops)
  647. {
  648. if (bdi_read_congested(target->bt_bdi))
  649. return;
  650. xfs_buf_read_map(target, map, nmaps,
  651. XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
  652. }
  653. /*
  654. * Read an uncached buffer from disk. Allocates and returns a locked
  655. * buffer containing the disk contents or nothing.
  656. */
  657. int
  658. xfs_buf_read_uncached(
  659. struct xfs_buftarg *target,
  660. xfs_daddr_t daddr,
  661. size_t numblks,
  662. int flags,
  663. struct xfs_buf **bpp,
  664. const struct xfs_buf_ops *ops)
  665. {
  666. struct xfs_buf *bp;
  667. *bpp = NULL;
  668. bp = xfs_buf_get_uncached(target, numblks, flags);
  669. if (!bp)
  670. return -ENOMEM;
  671. /* set up the buffer for a read IO */
  672. ASSERT(bp->b_map_count == 1);
  673. bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
  674. bp->b_maps[0].bm_bn = daddr;
  675. bp->b_flags |= XBF_READ;
  676. bp->b_ops = ops;
  677. xfs_buf_submit_wait(bp);
  678. if (bp->b_error) {
  679. int error = bp->b_error;
  680. xfs_buf_relse(bp);
  681. return error;
  682. }
  683. *bpp = bp;
  684. return 0;
  685. }
  686. /*
  687. * Return a buffer allocated as an empty buffer and associated to external
  688. * memory via xfs_buf_associate_memory() back to it's empty state.
  689. */
  690. void
  691. xfs_buf_set_empty(
  692. struct xfs_buf *bp,
  693. size_t numblks)
  694. {
  695. if (bp->b_pages)
  696. _xfs_buf_free_pages(bp);
  697. bp->b_pages = NULL;
  698. bp->b_page_count = 0;
  699. bp->b_addr = NULL;
  700. bp->b_length = numblks;
  701. bp->b_io_length = numblks;
  702. ASSERT(bp->b_map_count == 1);
  703. bp->b_bn = XFS_BUF_DADDR_NULL;
  704. bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
  705. bp->b_maps[0].bm_len = bp->b_length;
  706. }
  707. static inline struct page *
  708. mem_to_page(
  709. void *addr)
  710. {
  711. if ((!is_vmalloc_addr(addr))) {
  712. return virt_to_page(addr);
  713. } else {
  714. return vmalloc_to_page(addr);
  715. }
  716. }
  717. int
  718. xfs_buf_associate_memory(
  719. xfs_buf_t *bp,
  720. void *mem,
  721. size_t len)
  722. {
  723. int rval;
  724. int i = 0;
  725. unsigned long pageaddr;
  726. unsigned long offset;
  727. size_t buflen;
  728. int page_count;
  729. pageaddr = (unsigned long)mem & PAGE_MASK;
  730. offset = (unsigned long)mem - pageaddr;
  731. buflen = PAGE_ALIGN(len + offset);
  732. page_count = buflen >> PAGE_SHIFT;
  733. /* Free any previous set of page pointers */
  734. if (bp->b_pages)
  735. _xfs_buf_free_pages(bp);
  736. bp->b_pages = NULL;
  737. bp->b_addr = mem;
  738. rval = _xfs_buf_get_pages(bp, page_count);
  739. if (rval)
  740. return rval;
  741. bp->b_offset = offset;
  742. for (i = 0; i < bp->b_page_count; i++) {
  743. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  744. pageaddr += PAGE_SIZE;
  745. }
  746. bp->b_io_length = BTOBB(len);
  747. bp->b_length = BTOBB(buflen);
  748. return 0;
  749. }
  750. xfs_buf_t *
  751. xfs_buf_get_uncached(
  752. struct xfs_buftarg *target,
  753. size_t numblks,
  754. int flags)
  755. {
  756. unsigned long page_count;
  757. int error, i;
  758. struct xfs_buf *bp;
  759. DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
  760. /* flags might contain irrelevant bits, pass only what we care about */
  761. bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
  762. if (unlikely(bp == NULL))
  763. goto fail;
  764. page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
  765. error = _xfs_buf_get_pages(bp, page_count);
  766. if (error)
  767. goto fail_free_buf;
  768. for (i = 0; i < page_count; i++) {
  769. bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
  770. if (!bp->b_pages[i])
  771. goto fail_free_mem;
  772. }
  773. bp->b_flags |= _XBF_PAGES;
  774. error = _xfs_buf_map_pages(bp, 0);
  775. if (unlikely(error)) {
  776. xfs_warn(target->bt_mount,
  777. "%s: failed to map pages", __func__);
  778. goto fail_free_mem;
  779. }
  780. trace_xfs_buf_get_uncached(bp, _RET_IP_);
  781. return bp;
  782. fail_free_mem:
  783. while (--i >= 0)
  784. __free_page(bp->b_pages[i]);
  785. _xfs_buf_free_pages(bp);
  786. fail_free_buf:
  787. xfs_buf_free_maps(bp);
  788. kmem_zone_free(xfs_buf_zone, bp);
  789. fail:
  790. return NULL;
  791. }
  792. /*
  793. * Increment reference count on buffer, to hold the buffer concurrently
  794. * with another thread which may release (free) the buffer asynchronously.
  795. * Must hold the buffer already to call this function.
  796. */
  797. void
  798. xfs_buf_hold(
  799. xfs_buf_t *bp)
  800. {
  801. trace_xfs_buf_hold(bp, _RET_IP_);
  802. atomic_inc(&bp->b_hold);
  803. }
  804. /*
  805. * Release a hold on the specified buffer. If the hold count is 1, the buffer is
  806. * placed on LRU or freed (depending on b_lru_ref).
  807. */
  808. void
  809. xfs_buf_rele(
  810. xfs_buf_t *bp)
  811. {
  812. struct xfs_perag *pag = bp->b_pag;
  813. bool release;
  814. bool freebuf = false;
  815. trace_xfs_buf_rele(bp, _RET_IP_);
  816. if (!pag) {
  817. ASSERT(list_empty(&bp->b_lru));
  818. ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
  819. if (atomic_dec_and_test(&bp->b_hold)) {
  820. xfs_buf_ioacct_dec(bp);
  821. xfs_buf_free(bp);
  822. }
  823. return;
  824. }
  825. ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
  826. ASSERT(atomic_read(&bp->b_hold) > 0);
  827. release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
  828. spin_lock(&bp->b_lock);
  829. if (!release) {
  830. /*
  831. * Drop the in-flight state if the buffer is already on the LRU
  832. * and it holds the only reference. This is racy because we
  833. * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
  834. * ensures the decrement occurs only once per-buf.
  835. */
  836. if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
  837. xfs_buf_ioacct_dec(bp);
  838. goto out_unlock;
  839. }
  840. /* the last reference has been dropped ... */
  841. xfs_buf_ioacct_dec(bp);
  842. if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
  843. /*
  844. * If the buffer is added to the LRU take a new reference to the
  845. * buffer for the LRU and clear the (now stale) dispose list
  846. * state flag
  847. */
  848. if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
  849. bp->b_state &= ~XFS_BSTATE_DISPOSE;
  850. atomic_inc(&bp->b_hold);
  851. }
  852. spin_unlock(&pag->pag_buf_lock);
  853. } else {
  854. /*
  855. * most of the time buffers will already be removed from the
  856. * LRU, so optimise that case by checking for the
  857. * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
  858. * was on was the disposal list
  859. */
  860. if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
  861. list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
  862. } else {
  863. ASSERT(list_empty(&bp->b_lru));
  864. }
  865. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  866. rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
  867. spin_unlock(&pag->pag_buf_lock);
  868. xfs_perag_put(pag);
  869. freebuf = true;
  870. }
  871. out_unlock:
  872. spin_unlock(&bp->b_lock);
  873. if (freebuf)
  874. xfs_buf_free(bp);
  875. }
  876. /*
  877. * Lock a buffer object, if it is not already locked.
  878. *
  879. * If we come across a stale, pinned, locked buffer, we know that we are
  880. * being asked to lock a buffer that has been reallocated. Because it is
  881. * pinned, we know that the log has not been pushed to disk and hence it
  882. * will still be locked. Rather than continuing to have trylock attempts
  883. * fail until someone else pushes the log, push it ourselves before
  884. * returning. This means that the xfsaild will not get stuck trying
  885. * to push on stale inode buffers.
  886. */
  887. int
  888. xfs_buf_trylock(
  889. struct xfs_buf *bp)
  890. {
  891. int locked;
  892. locked = down_trylock(&bp->b_sema) == 0;
  893. if (locked) {
  894. XB_SET_OWNER(bp);
  895. trace_xfs_buf_trylock(bp, _RET_IP_);
  896. } else {
  897. trace_xfs_buf_trylock_fail(bp, _RET_IP_);
  898. }
  899. return locked;
  900. }
  901. /*
  902. * Lock a buffer object.
  903. *
  904. * If we come across a stale, pinned, locked buffer, we know that we
  905. * are being asked to lock a buffer that has been reallocated. Because
  906. * it is pinned, we know that the log has not been pushed to disk and
  907. * hence it will still be locked. Rather than sleeping until someone
  908. * else pushes the log, push it ourselves before trying to get the lock.
  909. */
  910. void
  911. xfs_buf_lock(
  912. struct xfs_buf *bp)
  913. {
  914. trace_xfs_buf_lock(bp, _RET_IP_);
  915. if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  916. xfs_log_force(bp->b_target->bt_mount, 0);
  917. down(&bp->b_sema);
  918. XB_SET_OWNER(bp);
  919. trace_xfs_buf_lock_done(bp, _RET_IP_);
  920. }
  921. void
  922. xfs_buf_unlock(
  923. struct xfs_buf *bp)
  924. {
  925. XB_CLEAR_OWNER(bp);
  926. up(&bp->b_sema);
  927. trace_xfs_buf_unlock(bp, _RET_IP_);
  928. }
  929. STATIC void
  930. xfs_buf_wait_unpin(
  931. xfs_buf_t *bp)
  932. {
  933. DECLARE_WAITQUEUE (wait, current);
  934. if (atomic_read(&bp->b_pin_count) == 0)
  935. return;
  936. add_wait_queue(&bp->b_waiters, &wait);
  937. for (;;) {
  938. set_current_state(TASK_UNINTERRUPTIBLE);
  939. if (atomic_read(&bp->b_pin_count) == 0)
  940. break;
  941. io_schedule();
  942. }
  943. remove_wait_queue(&bp->b_waiters, &wait);
  944. set_current_state(TASK_RUNNING);
  945. }
  946. /*
  947. * Buffer Utility Routines
  948. */
  949. void
  950. xfs_buf_ioend(
  951. struct xfs_buf *bp)
  952. {
  953. bool read = bp->b_flags & XBF_READ;
  954. trace_xfs_buf_iodone(bp, _RET_IP_);
  955. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  956. /*
  957. * Pull in IO completion errors now. We are guaranteed to be running
  958. * single threaded, so we don't need the lock to read b_io_error.
  959. */
  960. if (!bp->b_error && bp->b_io_error)
  961. xfs_buf_ioerror(bp, bp->b_io_error);
  962. /* Only validate buffers that were read without errors */
  963. if (read && !bp->b_error && bp->b_ops) {
  964. ASSERT(!bp->b_iodone);
  965. bp->b_ops->verify_read(bp);
  966. }
  967. if (!bp->b_error)
  968. bp->b_flags |= XBF_DONE;
  969. if (bp->b_iodone)
  970. (*(bp->b_iodone))(bp);
  971. else if (bp->b_flags & XBF_ASYNC)
  972. xfs_buf_relse(bp);
  973. else
  974. complete(&bp->b_iowait);
  975. }
  976. static void
  977. xfs_buf_ioend_work(
  978. struct work_struct *work)
  979. {
  980. struct xfs_buf *bp =
  981. container_of(work, xfs_buf_t, b_ioend_work);
  982. xfs_buf_ioend(bp);
  983. }
  984. static void
  985. xfs_buf_ioend_async(
  986. struct xfs_buf *bp)
  987. {
  988. INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
  989. queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
  990. }
  991. void
  992. xfs_buf_ioerror(
  993. xfs_buf_t *bp,
  994. int error)
  995. {
  996. ASSERT(error <= 0 && error >= -1000);
  997. bp->b_error = error;
  998. trace_xfs_buf_ioerror(bp, error, _RET_IP_);
  999. }
  1000. void
  1001. xfs_buf_ioerror_alert(
  1002. struct xfs_buf *bp,
  1003. const char *func)
  1004. {
  1005. xfs_alert(bp->b_target->bt_mount,
  1006. "metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
  1007. (__uint64_t)XFS_BUF_ADDR(bp), func, -bp->b_error, bp->b_length);
  1008. }
  1009. int
  1010. xfs_bwrite(
  1011. struct xfs_buf *bp)
  1012. {
  1013. int error;
  1014. ASSERT(xfs_buf_islocked(bp));
  1015. bp->b_flags |= XBF_WRITE;
  1016. bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
  1017. XBF_WRITE_FAIL | XBF_DONE);
  1018. error = xfs_buf_submit_wait(bp);
  1019. if (error) {
  1020. xfs_force_shutdown(bp->b_target->bt_mount,
  1021. SHUTDOWN_META_IO_ERROR);
  1022. }
  1023. return error;
  1024. }
  1025. static void
  1026. xfs_buf_bio_end_io(
  1027. struct bio *bio)
  1028. {
  1029. struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
  1030. /*
  1031. * don't overwrite existing errors - otherwise we can lose errors on
  1032. * buffers that require multiple bios to complete.
  1033. */
  1034. if (bio->bi_error)
  1035. cmpxchg(&bp->b_io_error, 0, bio->bi_error);
  1036. if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
  1037. invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
  1038. if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
  1039. xfs_buf_ioend_async(bp);
  1040. bio_put(bio);
  1041. }
  1042. static void
  1043. xfs_buf_ioapply_map(
  1044. struct xfs_buf *bp,
  1045. int map,
  1046. int *buf_offset,
  1047. int *count,
  1048. int op,
  1049. int op_flags)
  1050. {
  1051. int page_index;
  1052. int total_nr_pages = bp->b_page_count;
  1053. int nr_pages;
  1054. struct bio *bio;
  1055. sector_t sector = bp->b_maps[map].bm_bn;
  1056. int size;
  1057. int offset;
  1058. total_nr_pages = bp->b_page_count;
  1059. /* skip the pages in the buffer before the start offset */
  1060. page_index = 0;
  1061. offset = *buf_offset;
  1062. while (offset >= PAGE_SIZE) {
  1063. page_index++;
  1064. offset -= PAGE_SIZE;
  1065. }
  1066. /*
  1067. * Limit the IO size to the length of the current vector, and update the
  1068. * remaining IO count for the next time around.
  1069. */
  1070. size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
  1071. *count -= size;
  1072. *buf_offset += size;
  1073. next_chunk:
  1074. atomic_inc(&bp->b_io_remaining);
  1075. nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
  1076. bio = bio_alloc(GFP_NOIO, nr_pages);
  1077. bio->bi_bdev = bp->b_target->bt_bdev;
  1078. bio->bi_iter.bi_sector = sector;
  1079. bio->bi_end_io = xfs_buf_bio_end_io;
  1080. bio->bi_private = bp;
  1081. bio_set_op_attrs(bio, op, op_flags);
  1082. for (; size && nr_pages; nr_pages--, page_index++) {
  1083. int rbytes, nbytes = PAGE_SIZE - offset;
  1084. if (nbytes > size)
  1085. nbytes = size;
  1086. rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
  1087. offset);
  1088. if (rbytes < nbytes)
  1089. break;
  1090. offset = 0;
  1091. sector += BTOBB(nbytes);
  1092. size -= nbytes;
  1093. total_nr_pages--;
  1094. }
  1095. if (likely(bio->bi_iter.bi_size)) {
  1096. if (xfs_buf_is_vmapped(bp)) {
  1097. flush_kernel_vmap_range(bp->b_addr,
  1098. xfs_buf_vmap_len(bp));
  1099. }
  1100. submit_bio(bio);
  1101. if (size)
  1102. goto next_chunk;
  1103. } else {
  1104. /*
  1105. * This is guaranteed not to be the last io reference count
  1106. * because the caller (xfs_buf_submit) holds a count itself.
  1107. */
  1108. atomic_dec(&bp->b_io_remaining);
  1109. xfs_buf_ioerror(bp, -EIO);
  1110. bio_put(bio);
  1111. }
  1112. }
  1113. STATIC void
  1114. _xfs_buf_ioapply(
  1115. struct xfs_buf *bp)
  1116. {
  1117. struct blk_plug plug;
  1118. int op;
  1119. int op_flags = 0;
  1120. int offset;
  1121. int size;
  1122. int i;
  1123. /*
  1124. * Make sure we capture only current IO errors rather than stale errors
  1125. * left over from previous use of the buffer (e.g. failed readahead).
  1126. */
  1127. bp->b_error = 0;
  1128. /*
  1129. * Initialize the I/O completion workqueue if we haven't yet or the
  1130. * submitter has not opted to specify a custom one.
  1131. */
  1132. if (!bp->b_ioend_wq)
  1133. bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
  1134. if (bp->b_flags & XBF_WRITE) {
  1135. op = REQ_OP_WRITE;
  1136. if (bp->b_flags & XBF_SYNCIO)
  1137. op_flags = WRITE_SYNC;
  1138. if (bp->b_flags & XBF_FUA)
  1139. op_flags |= REQ_FUA;
  1140. if (bp->b_flags & XBF_FLUSH)
  1141. op_flags |= REQ_PREFLUSH;
  1142. /*
  1143. * Run the write verifier callback function if it exists. If
  1144. * this function fails it will mark the buffer with an error and
  1145. * the IO should not be dispatched.
  1146. */
  1147. if (bp->b_ops) {
  1148. bp->b_ops->verify_write(bp);
  1149. if (bp->b_error) {
  1150. xfs_force_shutdown(bp->b_target->bt_mount,
  1151. SHUTDOWN_CORRUPT_INCORE);
  1152. return;
  1153. }
  1154. } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
  1155. struct xfs_mount *mp = bp->b_target->bt_mount;
  1156. /*
  1157. * non-crc filesystems don't attach verifiers during
  1158. * log recovery, so don't warn for such filesystems.
  1159. */
  1160. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  1161. xfs_warn(mp,
  1162. "%s: no ops on block 0x%llx/0x%x",
  1163. __func__, bp->b_bn, bp->b_length);
  1164. xfs_hex_dump(bp->b_addr, 64);
  1165. dump_stack();
  1166. }
  1167. }
  1168. } else if (bp->b_flags & XBF_READ_AHEAD) {
  1169. op = REQ_OP_READ;
  1170. op_flags = REQ_RAHEAD;
  1171. } else {
  1172. op = REQ_OP_READ;
  1173. }
  1174. /* we only use the buffer cache for meta-data */
  1175. op_flags |= REQ_META;
  1176. /*
  1177. * Walk all the vectors issuing IO on them. Set up the initial offset
  1178. * into the buffer and the desired IO size before we start -
  1179. * _xfs_buf_ioapply_vec() will modify them appropriately for each
  1180. * subsequent call.
  1181. */
  1182. offset = bp->b_offset;
  1183. size = BBTOB(bp->b_io_length);
  1184. blk_start_plug(&plug);
  1185. for (i = 0; i < bp->b_map_count; i++) {
  1186. xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
  1187. if (bp->b_error)
  1188. break;
  1189. if (size <= 0)
  1190. break; /* all done */
  1191. }
  1192. blk_finish_plug(&plug);
  1193. }
  1194. /*
  1195. * Asynchronous IO submission path. This transfers the buffer lock ownership and
  1196. * the current reference to the IO. It is not safe to reference the buffer after
  1197. * a call to this function unless the caller holds an additional reference
  1198. * itself.
  1199. */
  1200. void
  1201. xfs_buf_submit(
  1202. struct xfs_buf *bp)
  1203. {
  1204. trace_xfs_buf_submit(bp, _RET_IP_);
  1205. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  1206. ASSERT(bp->b_flags & XBF_ASYNC);
  1207. /* on shutdown we stale and complete the buffer immediately */
  1208. if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  1209. xfs_buf_ioerror(bp, -EIO);
  1210. bp->b_flags &= ~XBF_DONE;
  1211. xfs_buf_stale(bp);
  1212. xfs_buf_ioend(bp);
  1213. return;
  1214. }
  1215. if (bp->b_flags & XBF_WRITE)
  1216. xfs_buf_wait_unpin(bp);
  1217. /* clear the internal error state to avoid spurious errors */
  1218. bp->b_io_error = 0;
  1219. /*
  1220. * The caller's reference is released during I/O completion.
  1221. * This occurs some time after the last b_io_remaining reference is
  1222. * released, so after we drop our Io reference we have to have some
  1223. * other reference to ensure the buffer doesn't go away from underneath
  1224. * us. Take a direct reference to ensure we have safe access to the
  1225. * buffer until we are finished with it.
  1226. */
  1227. xfs_buf_hold(bp);
  1228. /*
  1229. * Set the count to 1 initially, this will stop an I/O completion
  1230. * callout which happens before we have started all the I/O from calling
  1231. * xfs_buf_ioend too early.
  1232. */
  1233. atomic_set(&bp->b_io_remaining, 1);
  1234. xfs_buf_ioacct_inc(bp);
  1235. _xfs_buf_ioapply(bp);
  1236. /*
  1237. * If _xfs_buf_ioapply failed, we can get back here with only the IO
  1238. * reference we took above. If we drop it to zero, run completion so
  1239. * that we don't return to the caller with completion still pending.
  1240. */
  1241. if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
  1242. if (bp->b_error)
  1243. xfs_buf_ioend(bp);
  1244. else
  1245. xfs_buf_ioend_async(bp);
  1246. }
  1247. xfs_buf_rele(bp);
  1248. /* Note: it is not safe to reference bp now we've dropped our ref */
  1249. }
  1250. /*
  1251. * Synchronous buffer IO submission path, read or write.
  1252. */
  1253. int
  1254. xfs_buf_submit_wait(
  1255. struct xfs_buf *bp)
  1256. {
  1257. int error;
  1258. trace_xfs_buf_submit_wait(bp, _RET_IP_);
  1259. ASSERT(!(bp->b_flags & (_XBF_DELWRI_Q | XBF_ASYNC)));
  1260. if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  1261. xfs_buf_ioerror(bp, -EIO);
  1262. xfs_buf_stale(bp);
  1263. bp->b_flags &= ~XBF_DONE;
  1264. return -EIO;
  1265. }
  1266. if (bp->b_flags & XBF_WRITE)
  1267. xfs_buf_wait_unpin(bp);
  1268. /* clear the internal error state to avoid spurious errors */
  1269. bp->b_io_error = 0;
  1270. /*
  1271. * For synchronous IO, the IO does not inherit the submitters reference
  1272. * count, nor the buffer lock. Hence we cannot release the reference we
  1273. * are about to take until we've waited for all IO completion to occur,
  1274. * including any xfs_buf_ioend_async() work that may be pending.
  1275. */
  1276. xfs_buf_hold(bp);
  1277. /*
  1278. * Set the count to 1 initially, this will stop an I/O completion
  1279. * callout which happens before we have started all the I/O from calling
  1280. * xfs_buf_ioend too early.
  1281. */
  1282. atomic_set(&bp->b_io_remaining, 1);
  1283. _xfs_buf_ioapply(bp);
  1284. /*
  1285. * make sure we run completion synchronously if it raced with us and is
  1286. * already complete.
  1287. */
  1288. if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
  1289. xfs_buf_ioend(bp);
  1290. /* wait for completion before gathering the error from the buffer */
  1291. trace_xfs_buf_iowait(bp, _RET_IP_);
  1292. wait_for_completion(&bp->b_iowait);
  1293. trace_xfs_buf_iowait_done(bp, _RET_IP_);
  1294. error = bp->b_error;
  1295. /*
  1296. * all done now, we can release the hold that keeps the buffer
  1297. * referenced for the entire IO.
  1298. */
  1299. xfs_buf_rele(bp);
  1300. return error;
  1301. }
  1302. void *
  1303. xfs_buf_offset(
  1304. struct xfs_buf *bp,
  1305. size_t offset)
  1306. {
  1307. struct page *page;
  1308. if (bp->b_addr)
  1309. return bp->b_addr + offset;
  1310. offset += bp->b_offset;
  1311. page = bp->b_pages[offset >> PAGE_SHIFT];
  1312. return page_address(page) + (offset & (PAGE_SIZE-1));
  1313. }
  1314. /*
  1315. * Move data into or out of a buffer.
  1316. */
  1317. void
  1318. xfs_buf_iomove(
  1319. xfs_buf_t *bp, /* buffer to process */
  1320. size_t boff, /* starting buffer offset */
  1321. size_t bsize, /* length to copy */
  1322. void *data, /* data address */
  1323. xfs_buf_rw_t mode) /* read/write/zero flag */
  1324. {
  1325. size_t bend;
  1326. bend = boff + bsize;
  1327. while (boff < bend) {
  1328. struct page *page;
  1329. int page_index, page_offset, csize;
  1330. page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
  1331. page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
  1332. page = bp->b_pages[page_index];
  1333. csize = min_t(size_t, PAGE_SIZE - page_offset,
  1334. BBTOB(bp->b_io_length) - boff);
  1335. ASSERT((csize + page_offset) <= PAGE_SIZE);
  1336. switch (mode) {
  1337. case XBRW_ZERO:
  1338. memset(page_address(page) + page_offset, 0, csize);
  1339. break;
  1340. case XBRW_READ:
  1341. memcpy(data, page_address(page) + page_offset, csize);
  1342. break;
  1343. case XBRW_WRITE:
  1344. memcpy(page_address(page) + page_offset, data, csize);
  1345. }
  1346. boff += csize;
  1347. data += csize;
  1348. }
  1349. }
  1350. /*
  1351. * Handling of buffer targets (buftargs).
  1352. */
  1353. /*
  1354. * Wait for any bufs with callbacks that have been submitted but have not yet
  1355. * returned. These buffers will have an elevated hold count, so wait on those
  1356. * while freeing all the buffers only held by the LRU.
  1357. */
  1358. static enum lru_status
  1359. xfs_buftarg_wait_rele(
  1360. struct list_head *item,
  1361. struct list_lru_one *lru,
  1362. spinlock_t *lru_lock,
  1363. void *arg)
  1364. {
  1365. struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
  1366. struct list_head *dispose = arg;
  1367. if (atomic_read(&bp->b_hold) > 1) {
  1368. /* need to wait, so skip it this pass */
  1369. trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
  1370. return LRU_SKIP;
  1371. }
  1372. if (!spin_trylock(&bp->b_lock))
  1373. return LRU_SKIP;
  1374. /*
  1375. * clear the LRU reference count so the buffer doesn't get
  1376. * ignored in xfs_buf_rele().
  1377. */
  1378. atomic_set(&bp->b_lru_ref, 0);
  1379. bp->b_state |= XFS_BSTATE_DISPOSE;
  1380. list_lru_isolate_move(lru, item, dispose);
  1381. spin_unlock(&bp->b_lock);
  1382. return LRU_REMOVED;
  1383. }
  1384. void
  1385. xfs_wait_buftarg(
  1386. struct xfs_buftarg *btp)
  1387. {
  1388. LIST_HEAD(dispose);
  1389. int loop = 0;
  1390. /*
  1391. * First wait on the buftarg I/O count for all in-flight buffers to be
  1392. * released. This is critical as new buffers do not make the LRU until
  1393. * they are released.
  1394. *
  1395. * Next, flush the buffer workqueue to ensure all completion processing
  1396. * has finished. Just waiting on buffer locks is not sufficient for
  1397. * async IO as the reference count held over IO is not released until
  1398. * after the buffer lock is dropped. Hence we need to ensure here that
  1399. * all reference counts have been dropped before we start walking the
  1400. * LRU list.
  1401. */
  1402. while (percpu_counter_sum(&btp->bt_io_count))
  1403. delay(100);
  1404. drain_workqueue(btp->bt_mount->m_buf_workqueue);
  1405. /* loop until there is nothing left on the lru list. */
  1406. while (list_lru_count(&btp->bt_lru)) {
  1407. list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
  1408. &dispose, LONG_MAX);
  1409. while (!list_empty(&dispose)) {
  1410. struct xfs_buf *bp;
  1411. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1412. list_del_init(&bp->b_lru);
  1413. if (bp->b_flags & XBF_WRITE_FAIL) {
  1414. xfs_alert(btp->bt_mount,
  1415. "Corruption Alert: Buffer at block 0x%llx had permanent write failures!",
  1416. (long long)bp->b_bn);
  1417. xfs_alert(btp->bt_mount,
  1418. "Please run xfs_repair to determine the extent of the problem.");
  1419. }
  1420. xfs_buf_rele(bp);
  1421. }
  1422. if (loop++ != 0)
  1423. delay(100);
  1424. }
  1425. }
  1426. static enum lru_status
  1427. xfs_buftarg_isolate(
  1428. struct list_head *item,
  1429. struct list_lru_one *lru,
  1430. spinlock_t *lru_lock,
  1431. void *arg)
  1432. {
  1433. struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
  1434. struct list_head *dispose = arg;
  1435. /*
  1436. * we are inverting the lru lock/bp->b_lock here, so use a trylock.
  1437. * If we fail to get the lock, just skip it.
  1438. */
  1439. if (!spin_trylock(&bp->b_lock))
  1440. return LRU_SKIP;
  1441. /*
  1442. * Decrement the b_lru_ref count unless the value is already
  1443. * zero. If the value is already zero, we need to reclaim the
  1444. * buffer, otherwise it gets another trip through the LRU.
  1445. */
  1446. if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
  1447. spin_unlock(&bp->b_lock);
  1448. return LRU_ROTATE;
  1449. }
  1450. bp->b_state |= XFS_BSTATE_DISPOSE;
  1451. list_lru_isolate_move(lru, item, dispose);
  1452. spin_unlock(&bp->b_lock);
  1453. return LRU_REMOVED;
  1454. }
  1455. static unsigned long
  1456. xfs_buftarg_shrink_scan(
  1457. struct shrinker *shrink,
  1458. struct shrink_control *sc)
  1459. {
  1460. struct xfs_buftarg *btp = container_of(shrink,
  1461. struct xfs_buftarg, bt_shrinker);
  1462. LIST_HEAD(dispose);
  1463. unsigned long freed;
  1464. freed = list_lru_shrink_walk(&btp->bt_lru, sc,
  1465. xfs_buftarg_isolate, &dispose);
  1466. while (!list_empty(&dispose)) {
  1467. struct xfs_buf *bp;
  1468. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1469. list_del_init(&bp->b_lru);
  1470. xfs_buf_rele(bp);
  1471. }
  1472. return freed;
  1473. }
  1474. static unsigned long
  1475. xfs_buftarg_shrink_count(
  1476. struct shrinker *shrink,
  1477. struct shrink_control *sc)
  1478. {
  1479. struct xfs_buftarg *btp = container_of(shrink,
  1480. struct xfs_buftarg, bt_shrinker);
  1481. return list_lru_shrink_count(&btp->bt_lru, sc);
  1482. }
  1483. void
  1484. xfs_free_buftarg(
  1485. struct xfs_mount *mp,
  1486. struct xfs_buftarg *btp)
  1487. {
  1488. unregister_shrinker(&btp->bt_shrinker);
  1489. ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
  1490. percpu_counter_destroy(&btp->bt_io_count);
  1491. list_lru_destroy(&btp->bt_lru);
  1492. if (mp->m_flags & XFS_MOUNT_BARRIER)
  1493. xfs_blkdev_issue_flush(btp);
  1494. kmem_free(btp);
  1495. }
  1496. int
  1497. xfs_setsize_buftarg(
  1498. xfs_buftarg_t *btp,
  1499. unsigned int sectorsize)
  1500. {
  1501. /* Set up metadata sector size info */
  1502. btp->bt_meta_sectorsize = sectorsize;
  1503. btp->bt_meta_sectormask = sectorsize - 1;
  1504. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1505. xfs_warn(btp->bt_mount,
  1506. "Cannot set_blocksize to %u on device %pg",
  1507. sectorsize, btp->bt_bdev);
  1508. return -EINVAL;
  1509. }
  1510. /* Set up device logical sector size mask */
  1511. btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
  1512. btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
  1513. return 0;
  1514. }
  1515. /*
  1516. * When allocating the initial buffer target we have not yet
  1517. * read in the superblock, so don't know what sized sectors
  1518. * are being used at this early stage. Play safe.
  1519. */
  1520. STATIC int
  1521. xfs_setsize_buftarg_early(
  1522. xfs_buftarg_t *btp,
  1523. struct block_device *bdev)
  1524. {
  1525. return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
  1526. }
  1527. xfs_buftarg_t *
  1528. xfs_alloc_buftarg(
  1529. struct xfs_mount *mp,
  1530. struct block_device *bdev)
  1531. {
  1532. xfs_buftarg_t *btp;
  1533. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
  1534. btp->bt_mount = mp;
  1535. btp->bt_dev = bdev->bd_dev;
  1536. btp->bt_bdev = bdev;
  1537. btp->bt_bdi = blk_get_backing_dev_info(bdev);
  1538. if (xfs_setsize_buftarg_early(btp, bdev))
  1539. goto error;
  1540. if (list_lru_init(&btp->bt_lru))
  1541. goto error;
  1542. if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
  1543. goto error;
  1544. btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
  1545. btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
  1546. btp->bt_shrinker.seeks = DEFAULT_SEEKS;
  1547. btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
  1548. register_shrinker(&btp->bt_shrinker);
  1549. return btp;
  1550. error:
  1551. kmem_free(btp);
  1552. return NULL;
  1553. }
  1554. /*
  1555. * Add a buffer to the delayed write list.
  1556. *
  1557. * This queues a buffer for writeout if it hasn't already been. Note that
  1558. * neither this routine nor the buffer list submission functions perform
  1559. * any internal synchronization. It is expected that the lists are thread-local
  1560. * to the callers.
  1561. *
  1562. * Returns true if we queued up the buffer, or false if it already had
  1563. * been on the buffer list.
  1564. */
  1565. bool
  1566. xfs_buf_delwri_queue(
  1567. struct xfs_buf *bp,
  1568. struct list_head *list)
  1569. {
  1570. ASSERT(xfs_buf_islocked(bp));
  1571. ASSERT(!(bp->b_flags & XBF_READ));
  1572. /*
  1573. * If the buffer is already marked delwri it already is queued up
  1574. * by someone else for imediate writeout. Just ignore it in that
  1575. * case.
  1576. */
  1577. if (bp->b_flags & _XBF_DELWRI_Q) {
  1578. trace_xfs_buf_delwri_queued(bp, _RET_IP_);
  1579. return false;
  1580. }
  1581. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1582. /*
  1583. * If a buffer gets written out synchronously or marked stale while it
  1584. * is on a delwri list we lazily remove it. To do this, the other party
  1585. * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
  1586. * It remains referenced and on the list. In a rare corner case it
  1587. * might get readded to a delwri list after the synchronous writeout, in
  1588. * which case we need just need to re-add the flag here.
  1589. */
  1590. bp->b_flags |= _XBF_DELWRI_Q;
  1591. if (list_empty(&bp->b_list)) {
  1592. atomic_inc(&bp->b_hold);
  1593. list_add_tail(&bp->b_list, list);
  1594. }
  1595. return true;
  1596. }
  1597. /*
  1598. * Compare function is more complex than it needs to be because
  1599. * the return value is only 32 bits and we are doing comparisons
  1600. * on 64 bit values
  1601. */
  1602. static int
  1603. xfs_buf_cmp(
  1604. void *priv,
  1605. struct list_head *a,
  1606. struct list_head *b)
  1607. {
  1608. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1609. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1610. xfs_daddr_t diff;
  1611. diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
  1612. if (diff < 0)
  1613. return -1;
  1614. if (diff > 0)
  1615. return 1;
  1616. return 0;
  1617. }
  1618. /*
  1619. * submit buffers for write.
  1620. *
  1621. * When we have a large buffer list, we do not want to hold all the buffers
  1622. * locked while we block on the request queue waiting for IO dispatch. To avoid
  1623. * this problem, we lock and submit buffers in groups of 50, thereby minimising
  1624. * the lock hold times for lists which may contain thousands of objects.
  1625. *
  1626. * To do this, we sort the buffer list before we walk the list to lock and
  1627. * submit buffers, and we plug and unplug around each group of buffers we
  1628. * submit.
  1629. */
  1630. static int
  1631. xfs_buf_delwri_submit_buffers(
  1632. struct list_head *buffer_list,
  1633. struct list_head *wait_list)
  1634. {
  1635. struct xfs_buf *bp, *n;
  1636. LIST_HEAD (submit_list);
  1637. int pinned = 0;
  1638. struct blk_plug plug;
  1639. list_sort(NULL, buffer_list, xfs_buf_cmp);
  1640. blk_start_plug(&plug);
  1641. list_for_each_entry_safe(bp, n, buffer_list, b_list) {
  1642. if (!wait_list) {
  1643. if (xfs_buf_ispinned(bp)) {
  1644. pinned++;
  1645. continue;
  1646. }
  1647. if (!xfs_buf_trylock(bp))
  1648. continue;
  1649. } else {
  1650. xfs_buf_lock(bp);
  1651. }
  1652. /*
  1653. * Someone else might have written the buffer synchronously or
  1654. * marked it stale in the meantime. In that case only the
  1655. * _XBF_DELWRI_Q flag got cleared, and we have to drop the
  1656. * reference and remove it from the list here.
  1657. */
  1658. if (!(bp->b_flags & _XBF_DELWRI_Q)) {
  1659. list_del_init(&bp->b_list);
  1660. xfs_buf_relse(bp);
  1661. continue;
  1662. }
  1663. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1664. /*
  1665. * We do all IO submission async. This means if we need
  1666. * to wait for IO completion we need to take an extra
  1667. * reference so the buffer is still valid on the other
  1668. * side. We need to move the buffer onto the io_list
  1669. * at this point so the caller can still access it.
  1670. */
  1671. bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
  1672. bp->b_flags |= XBF_WRITE | XBF_ASYNC;
  1673. if (wait_list) {
  1674. xfs_buf_hold(bp);
  1675. list_move_tail(&bp->b_list, wait_list);
  1676. } else
  1677. list_del_init(&bp->b_list);
  1678. xfs_buf_submit(bp);
  1679. }
  1680. blk_finish_plug(&plug);
  1681. return pinned;
  1682. }
  1683. /*
  1684. * Write out a buffer list asynchronously.
  1685. *
  1686. * This will take the @buffer_list, write all non-locked and non-pinned buffers
  1687. * out and not wait for I/O completion on any of the buffers. This interface
  1688. * is only safely useable for callers that can track I/O completion by higher
  1689. * level means, e.g. AIL pushing as the @buffer_list is consumed in this
  1690. * function.
  1691. */
  1692. int
  1693. xfs_buf_delwri_submit_nowait(
  1694. struct list_head *buffer_list)
  1695. {
  1696. return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
  1697. }
  1698. /*
  1699. * Write out a buffer list synchronously.
  1700. *
  1701. * This will take the @buffer_list, write all buffers out and wait for I/O
  1702. * completion on all of the buffers. @buffer_list is consumed by the function,
  1703. * so callers must have some other way of tracking buffers if they require such
  1704. * functionality.
  1705. */
  1706. int
  1707. xfs_buf_delwri_submit(
  1708. struct list_head *buffer_list)
  1709. {
  1710. LIST_HEAD (wait_list);
  1711. int error = 0, error2;
  1712. struct xfs_buf *bp;
  1713. xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
  1714. /* Wait for IO to complete. */
  1715. while (!list_empty(&wait_list)) {
  1716. bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
  1717. list_del_init(&bp->b_list);
  1718. /* locking the buffer will wait for async IO completion. */
  1719. xfs_buf_lock(bp);
  1720. error2 = bp->b_error;
  1721. xfs_buf_relse(bp);
  1722. if (!error)
  1723. error = error2;
  1724. }
  1725. return error;
  1726. }
  1727. int __init
  1728. xfs_buf_init(void)
  1729. {
  1730. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1731. KM_ZONE_HWALIGN, NULL);
  1732. if (!xfs_buf_zone)
  1733. goto out;
  1734. return 0;
  1735. out:
  1736. return -ENOMEM;
  1737. }
  1738. void
  1739. xfs_buf_terminate(void)
  1740. {
  1741. kmem_zone_destroy(xfs_buf_zone);
  1742. }