xfs_buf.c 51 KB

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