xfs_buf.c 50 KB

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