xfs_buf.c 43 KB

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