xfs_buf.c 42 KB

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