xfs_buf.c 43 KB

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