xfs_buf.c 43 KB

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